Skip to content

Commit f9f3feb

Browse files
committed
Add solution and test-cases for problem 3100
1 parent df4288a commit f9f3feb

File tree

5 files changed

+51
-27
lines changed

5 files changed

+51
-27
lines changed
107 KB
Loading
137 KB
Loading

leetcode/3001-3100/3100.Water-Bottles-II/README.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
# [3100.Water Bottles II][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+
You are given two integers `numBottles` and `numExchange`.
5+
6+
`numBottles` represents the number of full water bottles that you initially have. In one operation, you can perform one of the following operations:
7+
8+
- Drink any number of full water bottles turning them into empty bottles.
9+
- Exchange `numExchange` empty bottles with one full water bottle. Then, increase `numExchange` by one.
10+
11+
Note that you cannot exchange multiple batches of empty bottles for the same value of `numExchange`. For example, if `numBottles == 3` and `numExchange == 1`, you cannot exchange `3` empty water bottles for `3` full bottles.
712

8-
**Example 1:**
13+
Return the **maximum** number of water bottles you can drink.
14+
15+
**Example 1:**
16+
17+
![1](./1.png)
918

1019
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
20+
Input: numBottles = 13, numExchange = 6
21+
Output: 15
22+
Explanation: The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk.
1323
```
1424

15-
## 题意
16-
> ...
25+
**Example 2:**
1726

18-
## 题解
27+
![2](./2.png)
1928

20-
### 思路1
21-
> ...
22-
Water Bottles II
23-
```go
2429
```
25-
30+
Input: numBottles = 10, numExchange = 3
31+
Output: 13
32+
Explanation: The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk.
33+
```
2634

2735
## 结语
2836

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(numBottles int, numExchange int) int {
4+
var ans int = numBottles
5+
drink := 0
6+
empty := numBottles
7+
for {
8+
if empty >= numExchange {
9+
drink++
10+
empty -= numExchange
11+
numExchange++
12+
continue
13+
}
14+
empty += drink
15+
ans += drink
16+
drink = 0
17+
if empty < numExchange {
18+
break
19+
}
20+
}
21+
return ans + drink
522
}

leetcode/3001-3100/3100.Water-Bottles-II/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
numBottles, numExchange int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 13, 6, 15},
17+
{"TestCase2", 10, 3, 13},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.numBottles, c.numExchange)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.numBottles, c.numExchange)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)