Skip to content

Commit 577d3d4

Browse files
committed
Update 1518 solution
1 parent c99d3ef commit 577d3d4

File tree

5 files changed

+86
-5
lines changed

5 files changed

+86
-5
lines changed

leetcode/1518.Water-Bottles/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [1518. Water Bottles](https://leetcode-cn.com/problems/water-bottles/)
1+
# [1518. Water Bottles](https://leetcode.com/problems/water-bottles/)
22

33
## 题目
44

@@ -51,8 +51,7 @@ Return the maximum number of water bottles you can drink.
5151

5252
## 解题思路
5353

54-
- 模拟
55-
首先我们一定可以喝到 numBottles 瓶酒,剩下 numBottles 个空瓶。接下来我们可以拿空瓶子换酒,每次拿出 numExchange 个瓶子换一瓶酒,然后再喝完这瓶酒,得到一个空瓶。这样模拟下去,直到所有的空瓶子小于numExchange结束
54+
- 模拟。首先我们一定可以喝到 numBottles 瓶酒,剩下 numBottles 个空瓶。接下来我们可以拿空瓶子换酒,每次拿出 numExchange 个瓶子换一瓶酒,然后再喝完这瓶酒,得到一个空瓶。这样模拟下去,直到所有的空瓶子小于numExchange结束。
5655

5756
## 代码
5857

website/content/ChapterFour/1500~1599/1512.Number-of-Good-Pairs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,5 @@ func numIdenticalPairs(nums []int) int {
7070
----------------------------------------------
7171
<div style="display: flex;justify-content: space-between;align-items: center;">
7272
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1400~1499/1486.XOR-Operation-in-an-Array/">⬅️上一页</a></p>
73-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1539.Kth-Missing-Positive-Number/">下一页➡️</a></p>
73+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1518.Water-Bottles/">下一页➡️</a></p>
7474
</div>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# [1518. Water Bottles](https://leetcode.com/problems/water-bottles/)
2+
3+
## 题目
4+
5+
Given numBottles full water bottles, you can exchange numExchange empty water bottles for one full water bottle.
6+
7+
The operation of drinking a full water bottle turns it into an empty bottle.
8+
9+
Return the maximum number of water bottles you can drink.
10+
11+
**Example 1**:
12+
13+
![https://assets.leetcode.com/uploads/2020/07/01/sample_1_1875.png](https://assets.leetcode.com/uploads/2020/07/01/sample_1_1875.png)
14+
15+
Input: numBottles = 9, numExchange = 3
16+
Output: 13
17+
Explanation: You can exchange 3 empty bottles to get 1 full water bottle.
18+
Number of water bottles you can drink: 9 + 3 + 1 = 13.
19+
20+
**Example 2**:
21+
22+
![https://assets.leetcode.com/uploads/2020/07/01/sample_2_1875.png](https://assets.leetcode.com/uploads/2020/07/01/sample_2_1875.png)
23+
24+
Input: numBottles = 15, numExchange = 4
25+
Output: 19
26+
Explanation: You can exchange 4 empty bottles to get 1 full water bottle.
27+
Number of water bottles you can drink: 15 + 3 + 1 = 19.
28+
29+
**Example 3**:
30+
31+
Input: numBottles = 5, numExchange = 5
32+
Output: 6
33+
34+
**Example 4**:
35+
36+
Input: numBottles = 2, numExchange = 3
37+
Output: 2
38+
39+
**Constraints:**
40+
41+
- 1 <= numBottles <= 100
42+
- 2 <= numExchange <= 100
43+
44+
## 题目大意
45+
46+
小区便利店正在促销,用 numExchange 个空酒瓶可以兑换一瓶新酒。你购入了 numBottles 瓶酒。
47+
48+
如果喝掉了酒瓶中的酒,那么酒瓶就会变成空的。
49+
50+
请你计算 最多 能喝到多少瓶酒。
51+
52+
## 解题思路
53+
54+
- 模拟。首先我们一定可以喝到 numBottles 瓶酒,剩下 numBottles 个空瓶。接下来我们可以拿空瓶子换酒,每次拿出 numExchange 个瓶子换一瓶酒,然后再喝完这瓶酒,得到一个空瓶。这样模拟下去,直到所有的空瓶子小于numExchange结束。
55+
56+
## 代码
57+
58+
```go
59+
package leetcode
60+
61+
func numWaterBottles(numBottles int, numExchange int) int {
62+
if numBottles < numExchange {
63+
return numBottles
64+
}
65+
quotient := numBottles / numExchange
66+
reminder := numBottles % numExchange
67+
ans := numBottles + quotient
68+
for quotient+reminder >= numExchange {
69+
quotient, reminder = (quotient+reminder)/numExchange, (quotient+reminder)%numExchange
70+
ans += quotient
71+
}
72+
return ans
73+
}
74+
```
75+
76+
77+
----------------------------------------------
78+
<div style="display: flex;justify-content: space-between;align-items: center;">
79+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1512.Number-of-Good-Pairs/">⬅️上一页</a></p>
80+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1539.Kth-Missing-Positive-Number/">下一页➡️</a></p>
81+
</div>

website/content/ChapterFour/1500~1599/1539.Kth-Missing-Positive-Number.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ func findKthPositive(arr []int, k int) int {
6565

6666
----------------------------------------------
6767
<div style="display: flex;justify-content: space-between;align-items: center;">
68-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1512.Number-of-Good-Pairs/">⬅️上一页</a></p>
68+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1518.Water-Bottles/">⬅️上一页</a></p>
6969
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1551.Minimum-Operations-to-Make-Array-Equal/">下一页➡️</a></p>
7070
</div>

website/content/ChapterTwo/Math.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ weight: 12
129129
|1442|Count Triplets That Can Form Two Arrays of Equal XOR|[Go]({{< relref "/ChapterFour/1400~1499/1442.Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR.md" >}})|Medium||||74.0%|
130130
|1486|XOR Operation in an Array|[Go]({{< relref "/ChapterFour/1400~1499/1486.XOR-Operation-in-an-Array.md" >}})|Easy||||84.1%|
131131
|1512|Number of Good Pairs|[Go]({{< relref "/ChapterFour/1500~1599/1512.Number-of-Good-Pairs.md" >}})|Easy||||87.7%|
132+
|1518|Water Bottles|[Go]({{< relref "/ChapterFour/1500~1599/1518.Water-Bottles.md" >}})|Easy||||60.4%|
132133
|1551|Minimum Operations to Make Array Equal|[Go]({{< relref "/ChapterFour/1500~1599/1551.Minimum-Operations-to-Make-Array-Equal.md" >}})|Medium||||80.7%|
133134
|1573|Number of Ways to Split a String|[Go]({{< relref "/ChapterFour/1500~1599/1573.Number-of-Ways-to-Split-a-String.md" >}})|Medium||||31.7%|
134135
|1648|Sell Diminishing-Valued Colored Balls|[Go]({{< relref "/ChapterFour/1600~1699/1648.Sell-Diminishing-Valued-Colored-Balls.md" >}})|Medium||||31.7%|

0 commit comments

Comments
 (0)