Skip to content

Commit da37d4c

Browse files
authored
feat: add solutions to lc problem: No.1553 (#2794)
No.1553.Minimum Number of Days to Eat N Oranges
1 parent 81dfcbd commit da37d4c

File tree

5 files changed

+104
-10
lines changed

5 files changed

+104
-10
lines changed

solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/README.md

+41-3
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,30 @@
6969

7070
### 方法一:记忆化搜索
7171

72+
根据题目描述,对于每个 $n$,我们可以选择三种方式之一:
73+
74+
1. 将 $n$ 减少 $1$;
75+
2. 如果 $n$ 能被 $2$ 整除,将 $n$ 的值除以 $2$;
76+
3. 如果 $n$ 能被 $3$ 整除,将 $n$ 的值除以 $3$。
77+
78+
因此,问题等价于求解通过上述三种方式,将 $n$ 减少到 $0$ 的最少天数。
79+
80+
我们设计一个函数 $dfs(n)$,表示将 $n$ 减少到 $0$ 的最少天数。函数 $dfs(n)$ 的执行过程如下:
81+
82+
1. 如果 $n < 2$,返回 $n$;
83+
2. 否则,我们可以先通过 $n \bmod 2$ 次操作 $1$,将 $n$ 减少到 $2$ 的倍数,然后执行操作 $2$,将 $n$ 减少到 $n/2$;我们也可以先通过 $n \bmod 3$ 次操作 $1$,将 $n$ 减少到 $3$ 的倍数,然后执行操作 $3$,将 $n$ 减少到 $n/3$。我们选择上述两种方式中最少的一种,即 $1 + \min(n \bmod 2 + dfs(n/2), n \bmod 3 + dfs(n/3))$。
84+
85+
为了避免重复计算,我们使用记忆化搜索的方法,将已经计算过的 $dfs(n)$ 的值存储在哈希表中。
86+
87+
时间复杂度 $O(\log^2 n)$,空间复杂度 $O(\log^2 n)$。
88+
7289
<!-- tabs:start -->
7390

7491
```python
7592
class Solution:
7693
def minDays(self, n: int) -> int:
7794
@cache
78-
def dfs(n):
95+
def dfs(n: int) -> int:
7996
if n < 2:
8097
return n
8198
return 1 + min(n % 2 + dfs(n // 2), n % 3 + dfs(n // 3))
@@ -115,8 +132,12 @@ public:
115132
}
116133

117134
int dfs(int n) {
118-
if (n < 2) return n;
119-
if (f.count(n)) return f[n];
135+
if (n < 2) {
136+
return n;
137+
}
138+
if (f.count(n)) {
139+
return f[n];
140+
}
120141
int res = 1 + min(n % 2 + dfs(n / 2), n % 3 + dfs(n / 3));
121142
f[n] = res;
122143
return res;
@@ -140,6 +161,23 @@ func minDays(n int) int {
140161
}
141162
```
142163

164+
```ts
165+
function minDays(n: number): number {
166+
const f: Record<number, number> = {};
167+
const dfs = (n: number): number => {
168+
if (n < 2) {
169+
return n;
170+
}
171+
if (f[n] !== undefined) {
172+
return f[n];
173+
}
174+
f[n] = 1 + Math.min((n % 2) + dfs((n / 2) | 0), (n % 3) + dfs((n / 3) | 0));
175+
return f[n];
176+
};
177+
return dfs(n);
178+
}
179+
```
180+
143181
<!-- tabs:end -->
144182

145183
<!-- end -->

solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/README_EN.md

+42-4
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,32 @@ You need at least 3 days to eat the 6 oranges.
5353

5454
## Solutions
5555

56-
### Solution 1
56+
### Solution 1: Memoization Search
57+
58+
According to the problem description, for each $n$, we can choose one of three ways:
59+
60+
1. Decrease $n$ by $1$;
61+
2. If $n$ can be divided by $2$, divide the value of $n$ by $2$;
62+
3. If $n$ can be divided by $3$, divide the value of $n$ by $3$.
63+
64+
Therefore, the problem is equivalent to finding the minimum number of days to reduce $n$ to $0$ through the above three ways.
65+
66+
We design a function $dfs(n)$, which represents the minimum number of days to reduce $n$ to $0$. The execution process of the function $dfs(n)$ is as follows:
67+
68+
1. If $n < 2$, return $n$;
69+
2. Otherwise, we can first reduce $n$ to a multiple of $2$ by $n \bmod 2$ operations of $1$, and then perform operation $2$ to reduce $n$ to $n/2$; we can also first reduce $n$ to a multiple of $3$ by $n \bmod 3$ operations of $1$, and then perform operation $3$ to reduce $n$ to $n/3$. We choose the minimum of the above two ways, that is, $1 + \min(n \bmod 2 + dfs(n/2), n \bmod 3 + dfs(n/3))$.
70+
71+
To avoid repeated calculations, we use the method of memoization search and store the calculated values of $dfs(n)$ in a hash table.
72+
73+
The time complexity is $O(\log^2 n)$, and the space complexity is $O(\log^2 n)$.
5774

5875
<!-- tabs:start -->
5976

6077
```python
6178
class Solution:
6279
def minDays(self, n: int) -> int:
6380
@cache
64-
def dfs(n):
81+
def dfs(n: int) -> int:
6582
if n < 2:
6683
return n
6784
return 1 + min(n % 2 + dfs(n // 2), n % 3 + dfs(n // 3))
@@ -101,8 +118,12 @@ public:
101118
}
102119

103120
int dfs(int n) {
104-
if (n < 2) return n;
105-
if (f.count(n)) return f[n];
121+
if (n < 2) {
122+
return n;
123+
}
124+
if (f.count(n)) {
125+
return f[n];
126+
}
106127
int res = 1 + min(n % 2 + dfs(n / 2), n % 3 + dfs(n / 3));
107128
f[n] = res;
108129
return res;
@@ -126,6 +147,23 @@ func minDays(n int) int {
126147
}
127148
```
128149

150+
```ts
151+
function minDays(n: number): number {
152+
const f: Record<number, number> = {};
153+
const dfs = (n: number): number => {
154+
if (n < 2) {
155+
return n;
156+
}
157+
if (f[n] !== undefined) {
158+
return f[n];
159+
}
160+
f[n] = 1 + Math.min((n % 2) + dfs((n / 2) | 0), (n % 3) + dfs((n / 3) | 0));
161+
return f[n];
162+
};
163+
return dfs(n);
164+
}
165+
```
166+
129167
<!-- tabs:end -->
130168

131169
<!-- end -->

solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/Solution.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ class Solution {
77
}
88

99
int dfs(int n) {
10-
if (n < 2) return n;
11-
if (f.count(n)) return f[n];
10+
if (n < 2) {
11+
return n;
12+
}
13+
if (f.count(n)) {
14+
return f[n];
15+
}
1216
int res = 1 + min(n % 2 + dfs(n / 2), n % 3 + dfs(n / 3));
1317
f[n] = res;
1418
return res;

solution/1500-1599/1553.Minimum Number of Days to Eat N Oranges/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def minDays(self, n: int) -> int:
33
@cache
4-
def dfs(n):
4+
def dfs(n: int) -> int:
55
if n < 2:
66
return n
77
return 1 + min(n % 2 + dfs(n // 2), n % 3 + dfs(n // 3))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function minDays(n: number): number {
2+
const f: Record<number, number> = {};
3+
const dfs = (n: number): number => {
4+
if (n < 2) {
5+
return n;
6+
}
7+
if (f[n] !== undefined) {
8+
return f[n];
9+
}
10+
f[n] = 1 + Math.min((n % 2) + dfs((n / 2) | 0), (n % 3) + dfs((n / 3) | 0));
11+
return f[n];
12+
};
13+
return dfs(n);
14+
}

0 commit comments

Comments
 (0)