Skip to content

Commit 476c154

Browse files
committed
[LeetCode Sync] Runtime - 44 ms (20.91%), Memory - 17.9 MB (26.34%)
1 parent a68cd98 commit 476c154

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

3336-water-bottles-ii/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<p>You are given two integers <code>numBottles</code> and <code>numExchange</code>.</p>
2+
3+
<p><code>numBottles</code> represents the number of full water bottles that you initially have. In one operation, you can perform one of the following operations:</p>
4+
5+
<ul>
6+
<li>Drink any number of full water bottles turning them into empty bottles.</li>
7+
<li>Exchange <code>numExchange</code> empty bottles with one full water bottle. Then, increase <code>numExchange</code> by one.</li>
8+
</ul>
9+
10+
<p>Note that you cannot exchange multiple batches of empty bottles for the same value of <code>numExchange</code>. For example, if <code>numBottles == 3</code> and <code>numExchange == 1</code>, you cannot exchange <code>3</code> empty water bottles for <code>3</code> full bottles.</p>
11+
12+
<p>Return <em>the <strong>maximum</strong> number of water bottles you can drink</em>.</p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong class="example">Example 1:</strong></p>
16+
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/28/exampleone1.png" style="width: 948px; height: 482px; padding: 10px; background: #fff; border-radius: .5rem;" />
17+
<pre>
18+
<strong>Input:</strong> numBottles = 13, numExchange = 6
19+
<strong>Output:</strong> 15
20+
<strong>Explanation:</strong> The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk.
21+
</pre>
22+
23+
<p><strong class="example">Example 2:</strong></p>
24+
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/28/example231.png" style="width: 990px; height: 642px; padding: 10px; background: #fff; border-radius: .5rem;" />
25+
<pre>
26+
<strong>Input:</strong> numBottles = 10, numExchange = 3
27+
<strong>Output:</strong> 13
28+
<strong>Explanation:</strong> The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk.
29+
</pre>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
<ul>
35+
<li><code>1 &lt;= numBottles &lt;= 100 </code></li>
36+
<li><code>1 &lt;= numExchange &lt;= 100</code></li>
37+
</ul>

3336-water-bottles-ii/solution.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def maxBottlesDrunk(self, numBottles: int, numExchange: int) -> int:
3+
a = 1
4+
b = 2 * numExchange - 3
5+
c = -2 * numBottles
6+
delta = b * b - 4 * a * c
7+
t = math.ceil((-b + math.sqrt(delta)) / (2 * a))
8+
return numBottles + t - 1

0 commit comments

Comments
 (0)