leetcode

1518. Water Bottles - Link

Question Description

There are numBottles water bottles that are initially full of water.
You can exchange numExchange empty water bottles from the market with one full water bottle.

The operation of drinking a full water bottle turns it into an empty bottle.

Given the two integers numBottles and numExchange, return the maximum number of water bottles you can drink.


Constraints


Approach

We simulate the process step by step:

  1. Drink all current bottles and increase the total count.
  2. Track how many empty bottles are collected.
  3. If empty bottles are enough for an exchange, convert them into new full bottles.
  4. Continue until you cannot exchange anymore.

This works because each exchange is independent and only depends on the count of empty bottles at each step.


Dry Run

Example Input: numBottles = 9, numExchange = 3

Step-by-step execution:

Final Answer = 13


Solution

class Solution {
    public int numWaterBottles(int numBottles, int numExchange) {
        int count = 0;
        int empty = 0;
        while (numBottles > 0) {
            count += numBottles;
            empty += numBottles;

            numBottles = empty / numExchange;
            empty = empty % numExchange;
        }
        return count;
    }
}

Time and Space Complexity