Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Last active October 2, 2025 22:39
Show Gist options
  • Select an option

  • Save Ifihan/0a98ae11f79e1fdf865c736f1367c13f to your computer and use it in GitHub Desktop.

Select an option

Save Ifihan/0a98ae11f79e1fdf865c736f1367c13f to your computer and use it in GitHub Desktop.

Question

Approach

I drink all the initial bottles first, keeping track of empties. Then, as long as I have at least numExchange empties, I perform a single exchange: I trade the empties for one full bottle, drink it, update the empties, and increase numExchange by one since future exchanges are harder. I repeat this process until I can no longer exchange.

Implementation

class Solution:
    def maxBottlesDrunk(self, numBottles: int, numExchange: int) -> int:
        total = numBottles
        empty = numBottles
        
        while empty >= numExchange:
            empty -= numExchange
            numBottles = 1
            total += 1
            empty += numBottles
            numExchange += 1
        
        return total

Complexities

  • Time: O(numBottles + numExchange) in the worst case (each loop step increases numExchange by 1)
  • Space: O(1)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment