Skip to content

Commit

Permalink
Merge pull request #878 from 0xff-dev/1553
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1553
  • Loading branch information
6boris committed Jun 6, 2024
2 parents e0f8926 + 56fe42c commit f3d5710
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
# [1553.Minimum Number of Days to Eat N Oranges][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
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: a = "11", b = "1"
Output: "100"
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.
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Minimum Number of Days to Eat N Oranges
```go
```

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.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(n int) int {
// dfs内存爆了,试试bfs
in := make(map[int]struct{})
queue := [][2]int{{n, 0}}
for len(queue) > 0 {
nq := make([][2]int, 0)
for _, cur := range queue {
if cur[0] == 1 {
return cur[1] + 1
}
next := cur[0] - 1
if _, ok := in[next]; !ok {
in[next] = struct{}{}
nq = append(nq, [2]int{next, cur[1] + 1})
}
if cur[0]%2 == 0 {
next = cur[0] / 2
if _, ok := in[next]; !ok {
in[next] = struct{}{}
nq = append(nq, [2]int{next, cur[1] + 1})
}
}

if cur[0]%3 == 0 {
next = cur[0] / 3
if _, ok := in[next]; !ok {
in[next] = struct{}{}
nq = append(nq, [2]int{next, cur[1] + 1})
}
}
}
queue = nq
}
return -1
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 2000000000, 32},
{"TestCase2", 10, 4},
{"TestCase3", 6, 3},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit f3d5710

Please sign in to comment.