1. 解题思路
这一题就是按照题目翻译一下,按照题中给出的规则不断进行bottle的兑换,直至无法兑换为止,即可得到最终的答案。
2. 代码实现
给出python代码实现如下:
class Solution:
def maxBottlesDrunk(self, numBottles: int, numExchange: int) -> int:
ans = 0
while numBottles >= numExchange:
ans += numExchange
numBottles -= numExchange
numExchange += 1
numBottles += 1
return ans + numBottles
提交代码评测得到:耗时32ms,占用内存16.5MB。