1553. Minimum Number of Days to Eat N Oranges LeetCode Solution

In this guide, you will get 1553. Minimum Number of Days to Eat N Oranges LeetCode Solution with the best time and space complexity. The solution to Minimum Number of Days to Eat N Oranges problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Number of Days to Eat N Oranges solution in C++
  4. Minimum Number of Days to Eat N Oranges solution in Java
  5. Minimum Number of Days to Eat N Oranges solution in Python
  6. Additional Resources
1553. Minimum Number of Days to Eat N Oranges LeetCode Solution image

Problem Statement of Minimum Number of Days to Eat N Oranges

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.

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.

See also  828. Count Unique Characters of All Substrings of a Given String LeetCode Solution

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.

Constraints:

1 <= n <= 2 * 109

Complexity Analysis

  • Time Complexity: O(\log^2 n)
  • Space Complexity: O(\log^2 n)

1553. Minimum Number of Days to Eat N Oranges LeetCode Solution in C++

class Solution {
 public:
  int minDays(int n) {
    if (n <= 1)
      return n;
    if (const auto it = mem.find(n); it != mem.cend())
      return it->second;
    return mem[n] = 1 + min(minDays(n / 3) + n % 3,  //
                            minDays(n / 2) + n % 2);
  }

 private:
  unordered_map<int, int> mem;
};
/* code provided by PROGIEZ */

1553. Minimum Number of Days to Eat N Oranges LeetCode Solution in Java

class Solution {
  public int minDays(int n) {
    if (n <= 1)
      return n;
    if (dp.containsKey(n))
      return dp.get(n);

    final int res = 1 + Math.min(minDays(n / 3) + n % 3, //
                                 minDays(n / 2) + n % 2);
    dp.put(n, res);
    return res;
  }

  private Map<Integer, Integer> dp = new HashMap<>();
}
// code provided by PROGIEZ

1553. Minimum Number of Days to Eat N Oranges LeetCode Solution in Python

class Solution:
  @functools.lru_cache(None)
  def minDays(self, n: int) -> int:
    if n <= 1:
      return n
    return 1 + min(self.minDays(n // 3) + n % 3,
                   self.minDays(n // 2) + n % 2)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.