Leetcode 3100. Water Bottles II

Problem

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 numExchange empty bottles with one full water bottle. Then, increase numExchange by 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.

Algorithm

The problem is similar to the previous bottle exchange scenario, but this time each exchange requires one additional bottle. By following the previous simulation method, simply increment the numExchange by one for each subsequent exchange.

Code

python3 复制代码
class Solution:
    def maxBottlesDrunk(self, numBottles: int, numExchange: int) -> int:
        ans, blank = 0, 0
        while numBottles or blank >= numExchange:
            # drink
            ans += numBottles
            blank += numBottles
            # exchange
            numBottles = 0
            while blank >= numExchange:
                numBottles += 1
                blank -= numExchange
                numExchange += 1

        return ans
相关推荐
sali-tec1 分钟前
C# 基于halcon的视觉工作流-章66 四目匹配
开发语言·人工智能·数码相机·算法·计算机视觉·c#
小明说Java7 分钟前
常见排序算法的实现
数据结构·算法·排序算法
行云流水20191 小时前
编程竞赛算法选择:理解时间复杂度提升解题效率
算法
smj2302_796826522 小时前
解决leetcode第3768题.固定长度子数组中的最小逆序对数目
python·算法·leetcode
cynicme3 小时前
力扣3531——统计被覆盖的建筑
算法·leetcode
core5123 小时前
深度解析DeepSeek-R1中GRPO强化学习算法
人工智能·算法·机器学习·deepseek·grpo
mit6.8243 小时前
计数if|
算法
a伊雪4 小时前
c++ 引用参数
c++·算法
圣保罗的大教堂4 小时前
leetcode 3531. 统计被覆盖的建筑 中等
leetcode