Skip to content

Add solution and test-cases for problem 3100 #1281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added leetcode/3001-3100/3100.Water-Bottles-II/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added leetcode/3001-3100/3100.Water-Bottles-II/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 22 additions & 14 deletions leetcode/3001-3100/3100.Water-Bottles-II/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
# [3100.Water Bottles II][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
You are given two integers `numBottles` and `numExchange`.

`numBottles` represents the number of full water bottles that you initially have. In one operation, you can perform one of the following operations:

- Drink any number of full water bottles turning them into empty bottles.
- Exchange `numExchange` empty bottles with one full water bottle. Then, increase `numExchange` by one.

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.

**Example 1:**
Return the **maximum** number of water bottles you can drink.

**Example 1:**

![1](./1.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: numBottles = 13, numExchange = 6
Output: 15
Explanation: The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk.
```

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

## 题解
![2](./2.png)

### 思路1
> ...
Water Bottles II
```go
```

Input: numBottles = 10, numExchange = 3
Output: 13
Explanation: The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk.
```

## 结语

Expand Down
21 changes: 19 additions & 2 deletions leetcode/3001-3100/3100.Water-Bottles-II/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(numBottles int, numExchange int) int {
var ans int = numBottles
drink := 0
empty := numBottles
for {
if empty >= numExchange {
drink++
empty -= numExchange
numExchange++
continue
}
empty += drink
ans += drink
drink = 0
if empty < numExchange {
break
}
}
return ans + drink
}
21 changes: 10 additions & 11 deletions leetcode/3001-3100/3100.Water-Bottles-II/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,30 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
numBottles, numExchange int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 13, 6, 15},
{"TestCase2", 10, 3, 13},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.numBottles, c.numExchange)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.numBottles, c.numExchange)
}
})
}
}

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

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