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.
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- Time: O(numBottles + numExchange) in the worst case (each loop step increases numExchange by 1)
- Space: O(1)