There are n oranges in the kitchen and you decided to eat some of these oranges every day as follows:
Eat one orange.
If the number of remaining oranges n is divisible by 2 then you can eat n / 2 oranges.
If the number of remaining oranges n is divisible by 3 then you can eat 2 * (n / 3) oranges.
You can only choose one of the actions per day.
Given the integer n, return the minimum number of days to eat n oranges.
Input: n = 10
Output: 4
Explanation: You have 10 oranges.
Day 1: Eat 1 orange, 10 - 1 = 9.
Day 2: Eat 6 oranges, 9 - 2*(9/3) = 9 - 6 = 3. (Since 9 is divisible by 3)
Day 3: Eat 2 oranges, 3 - 2*(3/3) = 3 - 2 = 1.
Day 4: Eat the last orange 1 - 1 = 0.
You need at least 4 days to eat the 10 oranges.
Example 2:
1
2
3
4
5
6
7
Input: n = 6
Output: 3
Explanation: You have 6 oranges.
Day 1: Eat 3 oranges, 6 - 6/2 = 6 - 3 = 3. (Since 6 is divisible by 2).
Day 2: Eat 2 oranges, 3 - 2*(3/3) = 3 - 2 = 1. (Since 3 is divisible by 3)
Day 3: Eat the last orange 1 - 1 = 0.
You need at least 3 days to eat the 6 oranges.
We want the minimum number of days to eat all oranges, and each day we can eat 1, n/2, or 2*(n/3) oranges if possible. Since the same subproblem can be reached by different paths, memoization helps avoid redundant calculations.
classSolution {
public: unordered_map<int, int> memo;
intminDays(int n) {
if (n <=1) return n;
if (memo.count(n)) return memo[n];
int ans =1+ min(
n %2+ minDays(n /2),
n %3+ minDays(n /3)
);
return memo[n] = ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
funcminDays(nint) int {
memo:=map[int]int{}
vardfsfunc(int) intdfs = func(xint) int {
ifx<=1 {
returnx }
ifv, ok:=memo[x]; ok {
returnv }
ans:=1+ min(x%2+dfs(x/2), x%3+dfs(x/3))
memo[x] = ansreturnans }
returndfs(n)
}
func min(a, bint) int { ifa < b { returna }; returnb }
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
Map<Integer, Integer> memo =new HashMap<>();
publicintminDays(int n) {
if (n <= 1) return n;
if (memo.containsKey(n)) return memo.get(n);
int ans = 1 + Math.min(
n % 2 + minDays(n / 2),
n % 3 + minDays(n / 3)
);
memo.put(n, ans);
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
val memo = mutableMapOf<Int, Int>()
funminDays(n: Int): Int {
if (n <=1) return n
memo[n]?.let { returnit }
val ans = 1 + minOf(
n % 2 + minDays(n / 2),
n % 3 + minDays(n / 3)
)
memo[n] = ans
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
defminDays(n: int) -> int:
memo: dict[int, int] = {}
defdfs(x: int) -> int:
if x <=1:
return x
if x in memo:
return memo[x]
ans =1+ min(x %2+ dfs(x //2), x %3+ dfs(x //3))
memo[x] = ans
return ans
return dfs(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
use std::collections::HashMap;
impl Solution {
pubfnmin_days(n: i32) -> i32 {
fndfs(x: i32, memo: &mut HashMap<i32, i32>) -> i32 {
if x <=1 { return x; }
iflet Some(&v) = memo.get(&x) { return v; }
let ans =1+ (x %2+ dfs(x /2, memo)).min(x %3+ dfs(x /3, memo));
memo.insert(x, ans);
ans
}
letmut memo = HashMap::new();
dfs(n, &mut memo)
}
}