Skip to content

Commit 57032bd

Browse files
authored
feat: add solutions to lc problem: No.3100 (doocs#4746)
No.3100.Water Bottles II
1 parent 14510ba commit 57032bd

File tree

4 files changed

+116
-9
lines changed

4 files changed

+116
-9
lines changed

solution/3100-3199/3100.Water Bottles II/README.md

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ tags:
6666

6767
### 方法一:模拟
6868

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

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

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

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

7878
<!-- tabs:start -->
7979

@@ -175,6 +175,45 @@ impl Solution {
175175
}
176176
```
177177

178+
#### C#
179+
180+
```cs
181+
public class Solution {
182+
public int MaxBottlesDrunk(int numBottles, int numExchange) {
183+
int ans = numBottles;
184+
while (numBottles >= numExchange) {
185+
numBottles -= numExchange;
186+
++numExchange;
187+
++ans;
188+
++numBottles;
189+
}
190+
return ans;
191+
}
192+
}
193+
```
194+
195+
#### PHP
196+
197+
```php
198+
class Solution {
199+
/**
200+
* @param Integer $numBottles
201+
* @param Integer $numExchange
202+
* @return Integer
203+
*/
204+
function maxBottlesDrunk($numBottles, $numExchange) {
205+
$ans = $numBottles;
206+
while ($numBottles >= $numExchange) {
207+
$numBottles -= $numExchange;
208+
$numExchange++;
209+
$ans++;
210+
$numBottles++;
211+
}
212+
return $ans;
213+
}
214+
}
215+
```
216+
178217
<!-- tabs:end -->
179218

180219
<!-- solution:end -->

solution/3100-3199/3100.Water Bottles II/README_EN.md

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ tags:
6565

6666
### Solution 1: Simulation
6767

68-
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:
68+
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:
6969

70-
- 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$.
71-
- 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.
70+
- 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$.
71+
- If we do not have $\textit{numExchange}$ empty bottles, we cannot exchange for more water and should stop.
7272

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

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

7777
<!-- tabs:start -->
7878

@@ -174,6 +174,45 @@ impl Solution {
174174
}
175175
```
176176

177+
#### C#
178+
179+
```cs
180+
public class Solution {
181+
public int MaxBottlesDrunk(int numBottles, int numExchange) {
182+
int ans = numBottles;
183+
while (numBottles >= numExchange) {
184+
numBottles -= numExchange;
185+
++numExchange;
186+
++ans;
187+
++numBottles;
188+
}
189+
return ans;
190+
}
191+
}
192+
```
193+
194+
#### PHP
195+
196+
```php
197+
class Solution {
198+
/**
199+
* @param Integer $numBottles
200+
* @param Integer $numExchange
201+
* @return Integer
202+
*/
203+
function maxBottlesDrunk($numBottles, $numExchange) {
204+
$ans = $numBottles;
205+
while ($numBottles >= $numExchange) {
206+
$numBottles -= $numExchange;
207+
$numExchange++;
208+
$ans++;
209+
$numBottles++;
210+
}
211+
return $ans;
212+
}
213+
}
214+
```
215+
177216
<!-- tabs:end -->
178217

179218
<!-- solution:end -->
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class Solution {
2+
public int MaxBottlesDrunk(int numBottles, int numExchange) {
3+
int ans = numBottles;
4+
while (numBottles >= numExchange) {
5+
numBottles -= numExchange;
6+
++numExchange;
7+
++ans;
8+
++numBottles;
9+
}
10+
return ans;
11+
}
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
/**
3+
* @param Integer $numBottles
4+
* @param Integer $numExchange
5+
* @return Integer
6+
*/
7+
function maxBottlesDrunk($numBottles, $numExchange) {
8+
$ans = $numBottles;
9+
while ($numBottles >= $numExchange) {
10+
$numBottles -= $numExchange;
11+
$numExchange++;
12+
$ans++;
13+
$numBottles++;
14+
}
15+
return $ans;
16+
}
17+
}

0 commit comments

Comments
 (0)