Skip to content

Commit ee15fd6

Browse files
committed
+ problem 1553
1 parent d74337f commit ee15fd6

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# 1553. Minimum Number of Days to Eat N Oranges
2+
There are `n` oranges in the kitchen and you decided to eat some of these oranges every day as follows:
3+
* Eat one orange.
4+
* If the number of remaining oranges `n` is divisible by `2` then you can eat `n / 2` oranges.
5+
* If the number of remaining oranges `n` is divisible by `3` then you can eat `2 * (n / 3)` oranges.
6+
7+
You can only choose one of the actions per day.
8+
9+
Given the integer `n`, return *the minimum number of days to eat* `n` *oranges*.
10+
11+
#### Example 1:
12+
<pre>
13+
<strong>Input:</strong> n = 10
14+
<strong>Output:</strong> 4
15+
<strong>Explanation:</strong> You have 10 oranges.
16+
Day 1: Eat 1 orange, 10 - 1 = 9.
17+
Day 2: Eat 6 oranges, 9 - 2*(9/3) = 9 - 6 = 3. (Since 9 is divisible by 3)
18+
Day 3: Eat 2 oranges, 3 - 2*(3/3) = 3 - 2 = 1.
19+
Day 4: Eat the last orange 1 - 1 = 0.
20+
You need at least 4 days to eat the 10 oranges.
21+
</pre>
22+
23+
#### Example 2:
24+
<pre>
25+
<strong>Input:</strong> n = 6
26+
<strong>Output:</strong> 3
27+
<strong>Explanation:</strong> You have 6 oranges.
28+
Day 1: Eat 3 oranges, 6 - 6/2 = 6 - 3 = 3. (Since 6 is divisible by 2).
29+
Day 2: Eat 2 oranges, 3 - 2*(3/3) = 3 - 2 = 1. (Since 3 is divisible by 3)
30+
Day 3: Eat the last orange 1 - 1 = 0.
31+
You need at least 3 days to eat the 6 oranges.
32+
</pre>
33+
34+
#### Constraints:
35+
* <code>1 <= n <= 2 * 10<sup>9</sup></code>
36+
37+
## Solutions (Python)
38+
39+
### 1. Solution
40+
```Python
41+
from functools import cache
42+
43+
44+
class Solution:
45+
@cache
46+
def minDays(self, n: int) -> int:
47+
if n <= 1:
48+
return n
49+
50+
return 1 + min(self.minDays(n // 2) + n % 2, self.minDays(n // 3) + n % 3)
51+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# 1553. 吃掉 N 个橘子的最少天数
2+
厨房里总共有 `n` 个橘子,你决定每一天选择如下方式之一吃这些橘子:
3+
* 吃掉一个橘子。
4+
* 如果剩余橘子数 `n` 能被 2 整除,那么你可以吃掉 `n/2` 个橘子。
5+
* 如果剩余橘子数 `n` 能被 3 整除,那么你可以吃掉 `2*(n/3)` 个橘子。
6+
7+
每天你只能从以上 3 种方案中选择一种方案。
8+
9+
请你返回吃掉所有 `n` 个橘子的最少天数。
10+
11+
#### 示例 1:
12+
<pre>
13+
<strong>输入:</strong> n = 10
14+
<strong>输出:</strong> 4
15+
<strong>解释:</strong> 你总共有 10 个橘子。
16+
第 1 天:吃 1 个橘子,剩余橘子数 10 - 1 = 9。
17+
第 2 天:吃 6 个橘子,剩余橘子数 9 - 2*(9/3) = 9 - 6 = 3。(9 可以被 3 整除)
18+
第 3 天:吃 2 个橘子,剩余橘子数 3 - 2*(3/3) = 3 - 2 = 1。
19+
第 4 天:吃掉最后 1 个橘子,剩余橘子数 1 - 1 = 0。
20+
你需要至少 4 天吃掉 10 个橘子。
21+
</pre>
22+
23+
#### 示例 2:
24+
<pre>
25+
<strong>输入:</strong> n = 6
26+
<strong>输出:</strong> 3
27+
<strong>解释:</strong> 你总共有 6 个橘子。
28+
第 1 天:吃 3 个橘子,剩余橘子数 6 - 6/2 = 6 - 3 = 3。(6 可以被 2 整除)
29+
第 2 天:吃 2 个橘子,剩余橘子数 3 - 2*(3/3) = 3 - 2 = 1。(3 可以被 3 整除)
30+
第 3 天:吃掉剩余 1 个橘子,剩余橘子数 1 - 1 = 0。
31+
你至少需要 3 天吃掉 6 个橘子。
32+
</pre>
33+
34+
#### 示例 3:
35+
<pre>
36+
<strong>输入:</strong> n = 1
37+
<strong>输出:</strong> 1
38+
</pre>
39+
40+
#### 示例 4:
41+
<pre>
42+
<strong>输入:</strong> n = 56
43+
<strong>输出:</strong> 6
44+
</pre>
45+
46+
#### 提示:
47+
* <code>1 <= n <= 2 * 10<sup>9</sup></code>
48+
49+
## 题解 (Python)
50+
51+
### 1. 题解
52+
```Python
53+
from functools import cache
54+
55+
56+
class Solution:
57+
@cache
58+
def minDays(self, n: int) -> int:
59+
if n <= 1:
60+
return n
61+
62+
return 1 + min(self.minDays(n // 2) + n % 2, self.minDays(n // 3) + n % 3)
63+
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from functools import cache
2+
3+
4+
class Solution:
5+
@cache
6+
def minDays(self, n: int) -> int:
7+
if n <= 1:
8+
return n
9+
10+
return 1 + min(self.minDays(n // 2) + n % 2, self.minDays(n // 3) + n % 3)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,7 @@
10371037
[1550][1550l]|[Three Consecutive Odds][1550] |![py]
10381038
[1551][1551l]|[Minimum Operations to Make Array Equal][1551] |![rb]&nbsp;&nbsp;![rs]
10391039
[1552][1552l]|[Magnetic Force Between Two Balls][1552] |![rs]
1040+
[1553][1553l]|[Minimum Number of Days to Eat N Oranges][1553] |![py]
10401041
[1556][1556l]|[Thousand Separator][1556] |![rs]
10411042
[1557][1557l]|[Minimum Number of Vertices to Reach All Nodes][1557] |![rs]
10421043
[1558][1558l]|[Minimum Numbers of Function Calls to Make Target Array][1558] |![rs]
@@ -2720,6 +2721,7 @@
27202721
[1550]:Problemset/1550-Three%20Consecutive%20Odds/README.md#1550-three-consecutive-odds
27212722
[1551]:Problemset/1551-Minimum%20Operations%20to%20Make%20Array%20Equal/README.md#1551-minimum-operations-to-make-array-equal
27222723
[1552]:Problemset/1552-Magnetic%20Force%20Between%20Two%20Balls/README.md#1552-magnetic-force-between-two-balls
2724+
[1553]:Problemset/1553-Minimum%20Number%20of%20Days%20to%20Eat%20N%20Oranges/README.md#1553-minimum-number-of-days-to-eat-n-oranges
27232725
[1556]:Problemset/1556-Thousand%20Separator/README.md#1556-thousand-separator
27242726
[1557]:Problemset/1557-Minimum%20Number%20of%20Vertices%20to%20Reach%20All%20Nodes/README.md#1557-minimum-number-of-vertices-to-reach-all-nodes
27252727
[1558]:Problemset/1558-Minimum%20Numbers%20of%20Function%20Calls%20to%20Make%20Target%20Array/README.md#1558-minimum-numbers-of-function-calls-to-make-target-array
@@ -4397,6 +4399,7 @@
43974399
[1550l]:https://leetcode.com/problems/three-consecutive-odds/
43984400
[1551l]:https://leetcode.com/problems/minimum-operations-to-make-array-equal/
43994401
[1552l]:https://leetcode.com/problems/magnetic-force-between-two-balls/
4402+
[1553l]:https://leetcode.com/problems/minimum-number-of-days-to-eat-n-oranges/
44004403
[1556l]:https://leetcode.com/problems/thousand-separator/
44014404
[1557l]:https://leetcode.com/problems/minimum-number-of-vertices-to-reach-all-nodes/
44024405
[1558l]:https://leetcode.com/problems/minimum-numbers-of-function-calls-to-make-target-array/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,7 @@
10371037
[1550][1550l]|[存在连续三个奇数的数组][1550] |![py]
10381038
[1551][1551l]|[使数组中所有元素相等的最小操作数][1551] |![rb]&nbsp;&nbsp;![rs]
10391039
[1552][1552l]|[两球之间的磁力][1552] |![rs]
1040+
[1553][1553l]|[吃掉 N 个橘子的最少天数][1553] |![py]
10401041
[1556][1556l]|[千位分隔数][1556] |![rs]
10411042
[1557][1557l]|[可以到达所有点的最少点数目][1557] |![rs]
10421043
[1558][1558l]|[得到目标数组的最少函数调用次数][1558] |![rs]
@@ -2720,6 +2721,7 @@
27202721
[1550]:Problemset/1550-Three%20Consecutive%20Odds/README_CN.md#1550-存在连续三个奇数的数组
27212722
[1551]:Problemset/1551-Minimum%20Operations%20to%20Make%20Array%20Equal/README_CN.md#1551-使数组中所有元素相等的最小操作数
27222723
[1552]:Problemset/1552-Magnetic%20Force%20Between%20Two%20Balls/README_CN.md#1552-两球之间的磁力
2724+
[1553]:Problemset/1553-Minimum%20Number%20of%20Days%20to%20Eat%20N%20Oranges/README_CN.md#1553-吃掉-n-个橘子的最少天数
27232725
[1556]:Problemset/1556-Thousand%20Separator/README_CN.md#1556-千位分隔数
27242726
[1557]:Problemset/1557-Minimum%20Number%20of%20Vertices%20to%20Reach%20All%20Nodes/README_CN.md#1557-可以到达所有点的最少点数目
27252727
[1558]:Problemset/1558-Minimum%20Numbers%20of%20Function%20Calls%20to%20Make%20Target%20Array/README_CN.md#1558-得到目标数组的最少函数调用次数
@@ -4397,6 +4399,7 @@
43974399
[1550l]:https://leetcode.cn/problems/three-consecutive-odds/
43984400
[1551l]:https://leetcode.cn/problems/minimum-operations-to-make-array-equal/
43994401
[1552l]:https://leetcode.cn/problems/magnetic-force-between-two-balls/
4402+
[1553l]:https://leetcode.cn/problems/minimum-number-of-days-to-eat-n-oranges/
44004403
[1556l]:https://leetcode.cn/problems/thousand-separator/
44014404
[1557l]:https://leetcode.cn/problems/minimum-number-of-vertices-to-reach-all-nodes/
44024405
[1558l]:https://leetcode.cn/problems/minimum-numbers-of-function-calls-to-make-target-array/

0 commit comments

Comments
 (0)