Minimum Number of Days to Eat N Oranges LeetCode solution in Java

1
class Solution {
2
HashMap<Integer, Integer> map;
3

4
public int minDays(int n) {
5
map = new HashMap<>();
6
map.put(0, 0);
7
map.put(1, 1);
8
return dp(n);
9
}
10

11
public int dp(int n) {
12
if (map.get(n) != null) return map.get(n);
13
int one = 1 + (n % 2) + dp(n / 2);
14
int two = 1 + (n % 3) + dp(n / 3);
15
map.put(n, Math.min(one, two));
16
return map.get(n);
17
}
18
}
19
// int one = 1+(n%2)+cache(n/2);
20
// int two = 1+(n%3)+cache(n/3);

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0