Update 1518 solution

This commit is contained in:
halfrost
2021-12-21 21:21:21 +08:00
parent c99d3ef68f
commit 577d3d465f
5 changed files with 86 additions and 5 deletions

View File

@ -1,4 +1,4 @@
# [1518. Water Bottles](https://leetcode-cn.com/problems/water-bottles/)
# [1518. Water Bottles](https://leetcode.com/problems/water-bottles/)
## 题目
@ -51,8 +51,7 @@ Return the maximum number of water bottles you can drink.
## 解题思路
- 模拟
首先我们一定可以喝到 numBottles 瓶酒,剩下 numBottles 个空瓶。接下来我们可以拿空瓶子换酒,每次拿出 numExchange 个瓶子换一瓶酒然后再喝完这瓶酒得到一个空瓶。这样模拟下去直到所有的空瓶子小于numExchange结束
- 模拟。首先我们一定可以喝到 numBottles 瓶酒,剩下 numBottles 个空瓶。接下来我们可以拿空瓶子换酒,每次拿出 numExchange 个瓶子换一瓶酒然后再喝完这瓶酒得到一个空瓶。这样模拟下去直到所有的空瓶子小于numExchange结束。
## 代码

View File

@ -70,5 +70,5 @@ func numIdenticalPairs(nums []int) int {
----------------------------------------------
<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1400~1499/1486.XOR-Operation-in-an-Array/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1539.Kth-Missing-Positive-Number/">下一页➡️</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1518.Water-Bottles/">下一页➡️</a></p>
</div>

View File

@ -0,0 +1,81 @@
# [1518. Water Bottles](https://leetcode.com/problems/water-bottles/)
## 题目
Given numBottles full water bottles, you can exchange numExchange empty water bottles for one full water bottle.
The operation of drinking a full water bottle turns it into an empty bottle.
Return the maximum number of water bottles you can drink.
**Example 1**:
![https://assets.leetcode.com/uploads/2020/07/01/sample_1_1875.png](https://assets.leetcode.com/uploads/2020/07/01/sample_1_1875.png)
Input: numBottles = 9, numExchange = 3
Output: 13
Explanation: You can exchange 3 empty bottles to get 1 full water bottle.
Number of water bottles you can drink: 9 + 3 + 1 = 13.
**Example 2**:
![https://assets.leetcode.com/uploads/2020/07/01/sample_2_1875.png](https://assets.leetcode.com/uploads/2020/07/01/sample_2_1875.png)
Input: numBottles = 15, numExchange = 4
Output: 19
Explanation: You can exchange 4 empty bottles to get 1 full water bottle.
Number of water bottles you can drink: 15 + 3 + 1 = 19.
**Example 3**:
Input: numBottles = 5, numExchange = 5
Output: 6
**Example 4**:
Input: numBottles = 2, numExchange = 3
Output: 2
**Constraints:**
- 1 <= numBottles <= 100
- 2 <= numExchange <= 100
## 题目大意
小区便利店正在促销,用 numExchange 个空酒瓶可以兑换一瓶新酒。你购入了 numBottles 瓶酒。
如果喝掉了酒瓶中的酒,那么酒瓶就会变成空的。
请你计算 最多 能喝到多少瓶酒。
## 解题思路
- 模拟。首先我们一定可以喝到 numBottles 瓶酒,剩下 numBottles 个空瓶。接下来我们可以拿空瓶子换酒,每次拿出 numExchange 个瓶子换一瓶酒然后再喝完这瓶酒得到一个空瓶。这样模拟下去直到所有的空瓶子小于numExchange结束。
## 代码
```go
package leetcode
func numWaterBottles(numBottles int, numExchange int) int {
if numBottles < numExchange {
return numBottles
}
quotient := numBottles / numExchange
reminder := numBottles % numExchange
ans := numBottles + quotient
for quotient+reminder >= numExchange {
quotient, reminder = (quotient+reminder)/numExchange, (quotient+reminder)%numExchange
ans += quotient
}
return ans
}
```
----------------------------------------------
<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1512.Number-of-Good-Pairs/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1539.Kth-Missing-Positive-Number/">下一页➡️</a></p>
</div>

View File

@ -65,6 +65,6 @@ func findKthPositive(arr []int, k int) int {
----------------------------------------------
<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1512.Number-of-Good-Pairs/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1518.Water-Bottles/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1551.Minimum-Operations-to-Make-Array-Equal/">下一页➡️</a></p>
</div>

View File

@ -129,6 +129,7 @@ weight: 12
|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%|
|1486|XOR Operation in an Array|[Go]({{< relref "/ChapterFour/1400~1499/1486.XOR-Operation-in-an-Array.md" >}})|Easy||||84.1%|
|1512|Number of Good Pairs|[Go]({{< relref "/ChapterFour/1500~1599/1512.Number-of-Good-Pairs.md" >}})|Easy||||87.7%|
|1518|Water Bottles|[Go]({{< relref "/ChapterFour/1500~1599/1518.Water-Bottles.md" >}})|Easy||||60.4%|
|1551|Minimum Operations to Make Array Equal|[Go]({{< relref "/ChapterFour/1500~1599/1551.Minimum-Operations-to-Make-Array-Equal.md" >}})|Medium||||80.7%|
|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%|
|1648|Sell Diminishing-Valued Colored Balls|[Go]({{< relref "/ChapterFour/1600~1699/1648.Sell-Diminishing-Valued-Colored-Balls.md" >}})|Medium||||31.7%|