diff --git a/solution/3100-3199/3100.Water Bottles II/README.md b/solution/3100-3199/3100.Water Bottles II/README.md index cd42288b095a4..ae4753b833cdc 100644 --- a/solution/3100-3199/3100.Water Bottles II/README.md +++ b/solution/3100-3199/3100.Water Bottles II/README.md @@ -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)$。 @@ -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; + } +} +``` + diff --git a/solution/3100-3199/3100.Water Bottles II/README_EN.md b/solution/3100-3199/3100.Water Bottles II/README_EN.md index e42d998765acf..8595dda44da2d 100644 --- a/solution/3100-3199/3100.Water Bottles II/README_EN.md +++ b/solution/3100-3199/3100.Water Bottles II/README_EN.md @@ -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)$. @@ -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; + } +} +``` + diff --git a/solution/3100-3199/3100.Water Bottles II/Solution.cs b/solution/3100-3199/3100.Water Bottles II/Solution.cs new file mode 100644 index 0000000000000..5d8ab0160d2c6 --- /dev/null +++ b/solution/3100-3199/3100.Water Bottles II/Solution.cs @@ -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; + } +} diff --git a/solution/3100-3199/3100.Water Bottles II/Solution.php b/solution/3100-3199/3100.Water Bottles II/Solution.php new file mode 100644 index 0000000000000..5f60990e0e016 --- /dev/null +++ b/solution/3100-3199/3100.Water Bottles II/Solution.php @@ -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; + } +}