diff --git a/leetcode/3001-3100/3100.Water-Bottles-II/1.png b/leetcode/3001-3100/3100.Water-Bottles-II/1.png new file mode 100644 index 000000000..98e58d583 Binary files /dev/null and b/leetcode/3001-3100/3100.Water-Bottles-II/1.png differ diff --git a/leetcode/3001-3100/3100.Water-Bottles-II/2.png b/leetcode/3001-3100/3100.Water-Bottles-II/2.png new file mode 100644 index 000000000..f5aa2dab7 Binary files /dev/null and b/leetcode/3001-3100/3100.Water-Bottles-II/2.png differ diff --git a/leetcode/3001-3100/3100.Water-Bottles-II/README.md b/leetcode/3001-3100/3100.Water-Bottles-II/README.md index 6030cf59f..b5059ccdd 100755 --- a/leetcode/3001-3100/3100.Water-Bottles-II/README.md +++ b/leetcode/3001-3100/3100.Water-Bottles-II/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/3001-3100/3100.Water-Bottles-II/Solution.go b/leetcode/3001-3100/3100.Water-Bottles-II/Solution.go index d115ccf5e..031fcea00 100644 --- a/leetcode/3001-3100/3100.Water-Bottles-II/Solution.go +++ b/leetcode/3001-3100/3100.Water-Bottles-II/Solution.go @@ -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 } diff --git a/leetcode/3001-3100/3100.Water-Bottles-II/Solution_test.go b/leetcode/3001-3100/3100.Water-Bottles-II/Solution_test.go index 14ff50eb4..e4bed4ecf 100644 --- a/leetcode/3001-3100/3100.Water-Bottles-II/Solution_test.go +++ b/leetcode/3001-3100/3100.Water-Bottles-II/Solution_test.go @@ -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() { }