← Home

1553. Minimum Number of Days to Eat N Oranges

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
DFS + memoizationC++Markdown
155

The trick here is to name the state correctly, then let the implementation follow. For 1553. Minimum Number of Days to Eat N Oranges, the solution in this repository is mainly a DFS + memoization solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: DFS + memoization, graph traversal, dynamic programming, greedy.

The notes already sitting in the source point us in the right direction:

  • DFS + memo
  • TLE
  • 76 / 176 test cases passed.

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are dfs, minDays.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
  • A set is doing the membership or uniqueness work, which keeps the main loop readable.
  • The queue gives the solution a level-by-level or frontier-style traversal.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//DFS + memo
02//TLE
03//76 / 176 test cases passed.
04//Runtime: 140 ms, faster than 28.57% of C++ online submissions for Minimum Number of Days to Eat N Oranges.
05//Memory Usage: 22.5 MB, less than 14.29% of C++ online submissions for Minimum Number of Days to Eat N Oranges.
06class Solution {
07public:
08    unordered_map<int, int> memo;
09    
10    int dfs(int n){
11        if(memo.find(n) != memo.end()){
12            return memo[n];
13        }
14        
15        int days = INT_MAX;
16        
17        days = min(days, dfs(n-1));
18        if(!(n&1)) days = min(days, dfs(n>>1));
19        if(n%3==0) days = min(days, dfs(n/3));
20        
21        return memo[n] = days+1;
22    }
23    int minDays(int n) {
24        memo[0] = 0;
25        return dfs(n);
26    }
27};
28
29//Greedy + DFS + memo
30//https://leetcode.com/problems/minimum-number-of-days-to-eat-n-oranges/discuss/794162/C%2B%2B-5-lines
31//Runtime: 28 ms, faster than 42.86% of C++ online submissions for Minimum Number of Days to Eat N Oranges.
32//Memory Usage: 10 MB, less than 57.14% of C++ online submissions for Minimum Number of Days to Eat N Oranges.
33class Solution {
34public:
35    unordered_map<int, int> memo;
36    
37    int dfs(int n){
38        if(memo.find(n) != memo.end()){
39            return memo[n];
40        }
41        
42        //eat one orange for n%2 days and then eat n/2 oranges
43        //eat one orange for n%3 days and then eat n*2/3 oranges
44        int days = min(n%2+dfs(n/2), n%3+dfs(n/3));
45        
46        // cout << n << ", " << days << endl;
47        return memo[n] = days+1;
48    };
49    
50    int minDays(int n) {
51        memo[0] = 0;
52        //this is also base case, o.w. memo[1] will be calculated as 2!
53        memo[1] = 1;
54        return dfs(n);
55    }
56};
57
58
59//BFS
60//Runtime: 140 ms, faster than 28.57% of C++ online submissions for Minimum Number of Days to Eat N Oranges.
61//Memory Usage: 22.5 MB, less than 14.29% of C++ online submissions for Minimum Number of Days to Eat N Oranges.
62class Solution {
63public:
64    int minDays(int n) {
65        queue<int> q;
66        
67        q.push(n);
68        int level = 0;
69        
70        unordered_set<int> visited;
71        visited.insert(n);
72        
73        while(!q.empty()){
74            int level_size = q.size();
75            
76            while(level_size-- > 0){
77                int cur = q.front(); q.pop();
78                
79                if(cur == 0){
80                    return level;
81                }
82                
83                if(visited.find(cur-1) == visited.end()){
84                    visited.insert(cur-1);
85                    q.push(cur-1);
86                }
87                if(!(cur&1) && visited.find(cur>>1) == visited.end()){
88                    visited.insert(cur>>1);
89                    q.push(cur>>1);
90                }
91                if(cur%3==0 && visited.find(cur%3) == visited.end()){
92                    visited.insert(cur/3);
93                    q.push(cur/3);
94                }
95            }
96            
97            ++level;
98        }
99        
100        return level;
101    }
102};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.