Skip to content
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
47 changes: 43 additions & 4 deletions solution/3100-3199/3100.Water Bottles II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ tags:

### 方法一:模拟

我们可以在一开始就喝掉所有的满水瓶,因此初始时我们喝到的水数量为 `numBottles`。然后我们不断地进行以下操作:
我们可以在一开始就喝掉所有的满水瓶,因此初始时我们喝到的水数量为 $\textit{numBottles}$。然后我们不断地进行以下操作:

- 如果当前有 `numExchange` 个空水瓶,我们就可以用它们换一瓶满水瓶,换完后,`numExchange` 的值增加 1。然后,我们喝掉这瓶水,喝到的水数量增加 $1$,空水瓶数量增加 $1$。
- 如果当前没有 `numExchange` 个空水瓶,那么我们就不能再换水了,此时我们就可以停止操作。
- 如果当前有 $\textit{numExchange}$ 个空水瓶,我们就可以用它们换一瓶满水瓶,换完后,$\textit{numExchange}$ 的值增加 $1$。然后,我们喝掉这瓶水,喝到的水数量增加 $1$,空水瓶数量增加 $1$。
- 如果当前没有 $\textit{numExchange}$ 个空水瓶,那么我们就不能再换水了,此时我们就可以停止操作。

我们不断地进行上述操作,直到我们无法再换水为止。最终我们喝到的水的数量就是答案。

时间复杂度 $O(\sqrt{numBottles})$,空间复杂度 $O(1)$。
时间复杂度 $O(\sqrt{n})$,其中 $n$ 是初始的满水瓶数量。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -175,6 +175,45 @@ impl Solution {
}
```

#### C#

```cs
public class Solution {
public int MaxBottlesDrunk(int numBottles, int numExchange) {
int ans = numBottles;
while (numBottles >= numExchange) {
numBottles -= numExchange;
++numExchange;
++ans;
++numBottles;
}
return ans;
}
}
```

#### PHP

```php
class Solution {
/**
* @param Integer $numBottles
* @param Integer $numExchange
* @return Integer
*/
function maxBottlesDrunk($numBottles, $numExchange) {
$ans = $numBottles;
while ($numBottles >= $numExchange) {
$numBottles -= $numExchange;
$numExchange++;
$ans++;
$numBottles++;
}
return $ans;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
49 changes: 44 additions & 5 deletions solution/3100-3199/3100.Water Bottles II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ tags:

### Solution 1: Simulation

We can drink all the full water bottles at the beginning, so the initial amount of water we drink is `numBottles`. Then we continuously perform the following operations:
We can drink all the full water bottles at the beginning, so initially the amount of water we drink is $\textit{numBottles}$. Then, we repeatedly perform the following operations:

- If we currently have `numExchange` empty water bottles, we can exchange them for a full water bottle, after which the value of `numExchange` increases by 1. Then, we drink this bottle of water, the amount of water we drink increases by $1$, and the number of empty water bottles increases by $1$.
- If we currently do not have `numExchange` empty water bottles, then we can no longer exchange for water, at which point we can stop the operation.
- If we currently have $\textit{numExchange}$ empty bottles, we can exchange them for one full bottle. After the exchange, the value of $\textit{numExchange}$ increases by $1$. Then, we drink this bottle, increasing the total amount of water drunk by $1$, and the number of empty bottles increases by $1$.
- If we do not have $\textit{numExchange}$ empty bottles, we cannot exchange for more water and should stop.

We continuously perform the above operations until we can no longer exchange for water. The final amount of water we drink is the answer.
We repeat the above process until we can no longer exchange bottles. The total amount of water drunk is the answer.

The time complexity is $O(\sqrt{numBottles})$ and the space complexity is $O(1)$.
The time complexity is $O(\sqrt{n})$, where $n$ is the initial number of full bottles. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -174,6 +174,45 @@ impl Solution {
}
```

#### C#

```cs
public class Solution {
public int MaxBottlesDrunk(int numBottles, int numExchange) {
int ans = numBottles;
while (numBottles >= numExchange) {
numBottles -= numExchange;
++numExchange;
++ans;
++numBottles;
}
return ans;
}
}
```

#### PHP

```php
class Solution {
/**
* @param Integer $numBottles
* @param Integer $numExchange
* @return Integer
*/
function maxBottlesDrunk($numBottles, $numExchange) {
$ans = $numBottles;
while ($numBottles >= $numExchange) {
$numBottles -= $numExchange;
$numExchange++;
$ans++;
$numBottles++;
}
return $ans;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
12 changes: 12 additions & 0 deletions solution/3100-3199/3100.Water Bottles II/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class Solution {
public int MaxBottlesDrunk(int numBottles, int numExchange) {
int ans = numBottles;
while (numBottles >= numExchange) {
numBottles -= numExchange;
++numExchange;
++ans;
++numBottles;
}
return ans;
}
}
17 changes: 17 additions & 0 deletions solution/3100-3199/3100.Water Bottles II/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
/**
* @param Integer $numBottles
* @param Integer $numExchange
* @return Integer
*/
function maxBottlesDrunk($numBottles, $numExchange) {
$ans = $numBottles;
while ($numBottles >= $numExchange) {
$numBottles -= $numExchange;
$numExchange++;
$ans++;
$numBottles++;
}
return $ans;
}
}