3100. Water Bottles II
題目
You are given two integers numBottles and numExchange.
numBottles represents the number of full water bottles that you initially have. In one operation, you can perform one of the following operations:
-
Drink any number of full water bottles turning them into empty bottles.
-
Exchange
numExchangeempty bottles with one full water bottle. Then, increasenumExchangeby one.
Note that you cannot exchange multiple batches of empty bottles for the same value of numExchange. For example, if numBottles == 3 and numExchange == 1, you cannot exchange 3 empty water bottles for 3 full bottles.
Return the maximum number of water bottles you can drink.
大致上的意思是一開始有 n 瓶飲料,喝完的空瓶可以換新的飲料,初始門檻是每 m 個空瓶換一瓶,但每換一瓶之後門檻就會 +1,所以不能用同樣的門檻一次換超過一瓶。
思路
解這題的思考模式比較接近實際的動作流程,按照 喝完飲料 -> 空瓶換新 -> 再喝 -> 再換 這樣的循環,一開始直接把手邊的飲料全部喝光變成空瓶,接下來就按照規則開始換瓶,每換一次就喝掉累積到空瓶上,同時兌換門檻也調高,迴圈執行到每辦法繼續換的時候結束。
Full Code
class Solution: def maxBottlesDrunk(self, numBottles: int, numExchange: int) -> int: bottleDrunk = numBottles # 把一開始給的全乾掉 emptyBottles = numBottles # 變成空瓶 while emptyBottles >= numExchange: # 檢查是否還能再換 emptyBottles -= numExchange # 空瓶換新 bottleDrunk += 1 # 喝掉 emptyBottles += 1 # 變成空瓶 numExchange += 1 # 兌換門檻+1 return bottleDrunk