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
相关推荐
谈笑也风生18 小时前
经典算法题型之排序算法(三)
java·算法·排序算法
大佬,救命!!!19 小时前
对算子shape相关的属性值自动化处理
python·算法·自动化·学习笔记·算子·用例脚本·算子形状
高山上有一只小老虎19 小时前
小红的推荐系统
java·算法
冰西瓜60019 小时前
贪心(一)——从动态规划到贪心 算法设计与分析 国科大
算法·贪心算法·动态规划
javachen__19 小时前
341-十道经典程序设计题目
数据结构·c++·算法
natide20 小时前
表示/嵌入差异-7-间隔/边际对齐(Alignment Margin)
人工智能·深度学习·算法·机器学习·自然语言处理·知识图谱
在风中的意志20 小时前
[数据库SQL] [leetcode-584] 584. 寻找用户推荐人
数据库·sql·leetcode
毅炼20 小时前
hot100打卡——day08
java·数据结构·算法·leetcode·深度优先
l1t20 小时前
DeepSeek总结的算法 X 与舞蹈链文章
前端·javascript·算法
gihigo199820 小时前
水声信号处理中DEMON谱分析的原理、实现与改进
算法·信号处理