Problem
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.
Algorithm
The process involves repeatedly exchanging empty bottles for new drinks, drinking them, and then continuing to exchange the newly obtained empty bottles. This cycle repeats until the number of empty bottles is insufficient for further exchanges.
Code
python3
class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
ans, blank = 0, 0
while numBottles or blank >= numExchange:
ans += numBottles
blank += numBottles
numBottles = blank // numExchange
blank %= numExchange
return ans