Water Bottles II - Problem
You are given two integers numBottles and numExchange.
numBottles represents the number of full water bottles that you initially have. In one operation, you can perform one of the following operations:
- Drink any number of full water bottles turning them into empty bottles.
- Exchange
numExchangeempty bottles with one full water bottle. Then, increasenumExchangeby one.
Note: You cannot exchange multiple batches of empty bottles for the same value of numExchange. For example, if numBottles == 3 and numExchange == 1, you cannot exchange 3 empty water bottles for 3 full bottles.
Return the maximum number of water bottles you can drink.
Input & Output
Example 1 — Basic Case
$
Input:
numBottles = 13, numExchange = 6
›
Output:
15
💡 Note:
Drink 13 bottles → 13 empties. Exchange 6 empties for 1 new bottle (cost becomes 7), leaving 7+1=8 empties. Drink 1 bottle → 8 empties. Exchange 7 empties for 1 new bottle (cost becomes 8), leaving 1+1=2 empties. Drink 1 bottle → 2 empties. Cannot exchange anymore (need 8, have 2). Total: 13+1+1=15
Example 2 — Higher Exchange Cost
$
Input:
numBottles = 10, numExchange = 3
›
Output:
13
💡 Note:
Drink 10 → 10 empties. Exchange 9 for 3 (cost=3), 1 empty left + 3 new = 4 empties total. Drink 3 → 7 empties. Exchange 4 for 1 (cost=4), 3 left + 1 new = 4 empties. Drink 1 → 5 empties. Exchange 5 for 1 (cost=5), 0 left + 1 new = 1 empty. Drink 1 → 1 empty. Total: 10+3+1+1=15
Example 3 — No Exchanges Possible
$
Input:
numBottles = 2, numExchange = 4
›
Output:
2
💡 Note:
Drink 2 bottles → 2 empties. Cannot exchange (need 4, have 2). Total: 2
Constraints
- 1 ≤ numBottles ≤ 100
- 1 ≤ numExchange ≤ 100
Visualization
Tap to expand
Understanding the Visualization
1
Input
numBottles=13 initial bottles, numExchange=6 exchange rate
2
Process
Drink bottles, exchange empties with increasing cost
3
Output
Total bottles drunk = 15
Key Takeaway
🎯 Key Insight: The exchange cost increases after each round, making fewer exchanges possible over time
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code