Water Bottles (LeetCode 1518): Solution and Explanation

Easy
Math
Simulation

You start with numBottles full bottles and can swap numExchange empty bottles for one full bottle. Every bottle you drink becomes an empty. Return the largest number of bottles you can drink.

Intuition

There is never a reason to hold on to a full bottle, so drink everything as soon as you have it. That leaves just one question per round: how many empties do you hold, and how many full bottles can they buy?

If you hold empty bottles, you can make empty // numExchange complete bundles and trade each for a full bottle. The remainder empty % numExchange is not wasted. Those bottles stay in your hand and join the empties produced by the bottles you are about to drink, which may complete another bundle in the next round.

The empties shrink by roughly a factor of numExchange each round, so the loop runs only a handful of times. When fewer than numExchange empties remain, no trade is possible and the count of bottles drunk is the answer.

Approach: Simulate the trade-in rounds

  1. Drink every starting bottle: drunk = numBottles, and you now hold empty = numBottles empties.
  2. While empty >= numExchange, trade new = empty // numExchange bundles for that many full bottles.
  3. Drink them: drunk += new.
  4. Update the empties: the empty % numExchange leftovers plus the new bottles you just emptied.
  5. When the loop stops, return drunk.

Step-by-step example

numBottles = 9, numExchange = 3
  1. Drink all 9: drunk = 9, empty = 9.
  2. 9 >= 3, so trade 3 bundles of 3 for 3 full bottles. No empties are left over.
  3. Drink the 3: drunk = 12. Empties are now 0 + 3 = 3.
  4. 3 >= 3, so trade 1 bundle for 1 full bottle and drink it: drunk = 13. Empties are now 0 + 1 = 1.
  5. 1 < 3, so no more trades. Return 13.

Code: Python, Java, C++

1class Solution:
2    def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
3        drunk = numBottles
4        empty = numBottles
5        while empty >= numExchange:
6            new = empty // numExchange
7            drunk += new
8            empty = empty % numExchange + new
9        return drunk
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Closed form O(1)
Simulate the trade-in rounds O(log n)
One bottle at a time O(n)
Time

O(log n)

Each round divides the number of empties by about numExchange, which is at least 2, so there are at most about log2(numBottles) rounds.

Space

O(1)

Only the counters drunk, empty and new are stored.

Other approaches

One bottle at a time (time O(n), space O(1))

Trade exactly numExchange empties for one bottle per iteration, drink it, and add one empty back. Each trade costs net numExchange - 1 empties. It is easy to reason about and still fast for these limits, but it takes up to about numBottles iterations.

Closed form (time O(1), space O(1))

Each trade consumes numExchange - 1 empties net, and you can keep trading while you hold at least numExchange. That gives numBottles + (numBottles - 1) // (numExchange - 1) without any loop.

Common mistakes

  • Throwing away the leftover empties (empty % numExchange) after each trade. They must carry into the next round, or examples like 15 and 4 come out one bottle short.
  • Forgetting that the bottles bought in a round become empties once drunk.
  • Writing the closed form as numBottles / (numExchange - 1) without the minus one, which counts a trade you cannot actually make.

Frequently asked questions

Why should I drink every full bottle immediately?

A full bottle cannot be traded, and drinking it both counts toward the answer and adds an empty. Holding it only delays trades.

Where does the formula numBottles + (numBottles - 1) // (numExchange - 1) come from?

Trading numExchange empties for a bottle you then drink leaves you with one empty, so each trade costs numExchange - 1 empties net. You can trade while at least numExchange empties remain, which allows (numBottles - 1) // (numExchange - 1) trades.

Can I borrow a bottle to finish a bundle?

Not in this problem. The market only accepts complete groups of numExchange empties you actually hold.

What is the complexity of the simulation?

O(log numBottles) time, because the empties drop by a factor of at least 2 each round, and O(1) space.