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
相关推荐
来一碗刘肉面2 小时前
栈的应用(表达式求值)
数据结构·算法
xiaowang1234shs3 小时前
怪兽轻断食技术深度测评:从断食计时引擎到AI识别算法的工程实践解析
数据库·人工智能·算法·macos·机器学习·p2p·visual studio
小果因子实验室3 小时前
量化研究--策略迁移算法1研究
算法
Web极客码3 小时前
如何用三段式确定性剪枝,为 LLM Agent 砍掉 35% 的 Token 成本?
服务器·人工智能·算法·机器学习
程序猿乐锅3 小时前
【数据结构与算法 | 第六篇】力扣1109,1094差分数组
java·算法·leetcode
wabs6664 小时前
关于图论【广度优先搜索的理论基础】
算法·图论·宽度优先
孤魂2334 小时前
逻辑回归算法
算法·机器学习·逻辑回归
weixin_377634844 小时前
【多模型预测】 如何合理融合多个预测分值
算法·机器学习·概率论·预测·agent预测
无风听海4 小时前
Claude Agent Skills 的四种设计模式;从渐进式披露到最小权限
java·算法·设计模式
37.2℃9954 小时前
专业的Claude Design解决方案
前端·算法