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 eatnoranges.
Example 1:
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:
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.
Problem summary: 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.
Baseline thinking
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Dynamic Programming
Example 1
10
Example 2
6
Step 02
Core Insight
What unlocks the optimal approach
In each step, choose between 2 options: minOranges = 1 + min( (n%2) + f(n/2), (n%3) + f(n/3) ) where f(n) is the minimum number of days to eat n oranges.
Interview move: turn each hint into an invariant you can check after every iteration/recursion step.
Step 03
Algorithm Walkthrough
Iteration Checklist
Define state (indices, window, stack, map, DP cell, or recursion frame).
Apply one transition step and update the invariant.
Record answer candidate when condition is met.
Continue until all input is consumed.
Use the first example testcase as your mental trace to verify each transition.
Step 04
Edge Cases
Minimum Input
Single element / shortest valid input
Validate boundary behavior before entering the main loop or recursion.
Duplicates & Repeats
Repeated values / repeated states
Decide whether duplicates should be merged, skipped, or counted explicitly.
Extreme Constraints
Largest constraint values
Re-check complexity target against constraints to avoid time-limit issues.
Invalid / Corner Shape
Empty collections, zeros, or disconnected structures
Handle special-case structure before the core algorithm path.
Step 05
Full Annotated Code
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1553: Minimum Number of Days to Eat N Oranges
class Solution {
private Map<Integer, Integer> f = new HashMap<>();
public int minDays(int n) {
return dfs(n);
}
private int dfs(int n) {
if (n < 2) {
return n;
}
if (f.containsKey(n)) {
return f.get(n);
}
int res = 1 + Math.min(n % 2 + dfs(n / 2), n % 3 + dfs(n / 3));
f.put(n, res);
return res;
}
}
// Accepted solution for LeetCode #1553: Minimum Number of Days to Eat N Oranges
func minDays(n int) int {
f := map[int]int{0: 0, 1: 1}
var dfs func(int) int
dfs = func(n int) int {
if v, ok := f[n]; ok {
return v
}
res := 1 + min(n%2+dfs(n/2), n%3+dfs(n/3))
f[n] = res
return res
}
return dfs(n)
}
# Accepted solution for LeetCode #1553: Minimum Number of Days to Eat N Oranges
class Solution:
def minDays(self, n: int) -> int:
@cache
def dfs(n: int) -> int:
if n < 2:
return n
return 1 + min(n % 2 + dfs(n // 2), n % 3 + dfs(n // 3))
return dfs(n)
// Accepted solution for LeetCode #1553: Minimum Number of Days to Eat N Oranges
struct Solution;
use std::collections::HashMap;
impl Solution {
fn min_days(n: i32) -> i32 {
let mut memo: HashMap<i32, i32> = HashMap::new();
Self::dp(n, &mut memo)
}
fn dp(n: i32, memo: &mut HashMap<i32, i32>) -> i32 {
if n <= 1 {
n
} else {
if let Some(&res) = memo.get(&n) {
return res;
}
let res = 1 + (n % 2 + Self::dp(n / 2, memo)).min(n % 3 + Self::dp(n / 3, memo));
memo.insert(n, res);
res
}
}
}
#[test]
fn test() {
let n = 10;
let res = 4;
assert_eq!(Solution::min_days(n), res);
let n = 6;
let res = 3;
assert_eq!(Solution::min_days(n), res);
let n = 1;
let res = 1;
assert_eq!(Solution::min_days(n), res);
let n = 56;
let res = 6;
assert_eq!(Solution::min_days(n), res);
let n = 9209408;
let res = 23;
assert_eq!(Solution::min_days(n), res);
let n = 84806671;
let res = 32;
assert_eq!(Solution::min_days(n), res);
}
// Accepted solution for LeetCode #1553: Minimum Number of Days to Eat N Oranges
function minDays(n: number): number {
const f: Record<number, number> = {};
const dfs = (n: number): number => {
if (n < 2) {
return n;
}
if (f[n] !== undefined) {
return f[n];
}
f[n] = 1 + Math.min((n % 2) + dfs((n / 2) | 0), (n % 3) + dfs((n / 3) | 0));
return f[n];
};
return dfs(n);
}
Step 06
Interactive Study Demo
Use this to step through a reusable interview workflow for this problem.
Press Step or Run All to begin.
Step 07
Complexity Analysis
Time
O(log^2 n)
Space
O(log^2 n)
Approach Breakdown
RECURSIVE
O(2ⁿ) time
O(n) space
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
DYNAMIC PROGRAMMING
O(n × m) time
O(n × m) space
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Shortcut: Count your DP state dimensions → that’s your time. Can you drop one? That’s your space optimization.
Coach Notes
Common Mistakes
Review these before coding to avoid predictable interview regressions.
State misses one required dimension
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.