Skip to content

LeetCode-1518 换酒问题

2021-12-17-view(s)-comment(s)- min read

题目

在这里插入图片描述在这里插入图片描述

分析

酒 -> 空瓶 -> 酒 -> 空瓶 这太递归了。

代码

python
class Solution(object):
    def numWaterBottles(self, numBottles, numExchange):
        """
        :type numBottles: int
        :type numExchange: int
        :rtype: int
        """
        exchange = numBottles//numExchange
        if exchange==0:
            return numBottles
        else:
            left = numBottles%numExchange
            return numBottles + self.numWaterBottles(left+exchange, numExchange) - left