Skip to content

Commit 56fe42c

Browse files
committed
Add solution and test-cases for problem 1553
1 parent 09a1dcb commit 56fe42c

File tree

3 files changed

+67
-23
lines changed

3 files changed

+67
-23
lines changed

leetcode/1501-1600/1553.Minimum-Number-of-Days-to-Eat-N-Oranges/README.md

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
# [1553.Minimum Number of Days to Eat N Oranges][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
There are `n` oranges in the kitchen and you decided to eat some of these oranges every day as follows:
5+
6+
- Eat one orange.
7+
- If the number of remaining oranges `n` is divisible by `2` then you can eat `n / 2` oranges.
8+
- If the number of remaining oranges `n` is divisible by `3` then you can eat `2 * (n / 3)` oranges.
9+
10+
You can only choose one of the actions per day.
11+
12+
Given the integer `n`, return the minimum number of days to eat `n` oranges.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: n = 10
18+
Output: 4
19+
Explanation: You have 10 oranges.
20+
Day 1: Eat 1 orange, 10 - 1 = 9.
21+
Day 2: Eat 6 oranges, 9 - 2*(9/3) = 9 - 6 = 3. (Since 9 is divisible by 3)
22+
Day 3: Eat 2 oranges, 3 - 2*(3/3) = 3 - 2 = 1.
23+
Day 4: Eat the last orange 1 - 1 = 0.
24+
You need at least 4 days to eat the 10 oranges.
1325
```
1426

15-
## 题意
16-
> ...
27+
**Example 2:**
1728

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Minimum Number of Days to Eat N Oranges
23-
```go
2429
```
25-
30+
Input: n = 6
31+
Output: 3
32+
Explanation: You have 6 oranges.
33+
Day 1: Eat 3 oranges, 6 - 6/2 = 6 - 3 = 3. (Since 6 is divisible by 2).
34+
Day 2: Eat 2 oranges, 3 - 2*(3/3) = 3 - 2 = 1. (Since 3 is divisible by 3)
35+
Day 3: Eat the last orange 1 - 1 = 0.
36+
You need at least 3 days to eat the 6 oranges.
37+
```
2638

2739
## 结语
2840

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int) int {
4+
// dfs内存爆了,试试bfs
5+
in := make(map[int]struct{})
6+
queue := [][2]int{{n, 0}}
7+
for len(queue) > 0 {
8+
nq := make([][2]int, 0)
9+
for _, cur := range queue {
10+
if cur[0] == 1 {
11+
return cur[1] + 1
12+
}
13+
next := cur[0] - 1
14+
if _, ok := in[next]; !ok {
15+
in[next] = struct{}{}
16+
nq = append(nq, [2]int{next, cur[1] + 1})
17+
}
18+
if cur[0]%2 == 0 {
19+
next = cur[0] / 2
20+
if _, ok := in[next]; !ok {
21+
in[next] = struct{}{}
22+
nq = append(nq, [2]int{next, cur[1] + 1})
23+
}
24+
}
25+
26+
if cur[0]%3 == 0 {
27+
next = cur[0] / 3
28+
if _, ok := in[next]; !ok {
29+
in[next] = struct{}{}
30+
nq = append(nq, [2]int{next, cur[1] + 1})
31+
}
32+
}
33+
}
34+
queue = nq
35+
}
36+
return -1
537
}

leetcode/1501-1600/1553.Minimum-Number-of-Days-to-Eat-N-Oranges/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 2000000000, 32},
17+
{"TestCase2", 10, 4},
18+
{"TestCase3", 6, 3},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)