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-tec2 分钟前
C# 基于OpenCv的视觉工作流-章75-线-线角度
图像处理·人工智能·opencv·算法·计算机视觉
大熊背12 分钟前
Binning模式下和Normal模式下加权平均亮度差异分析以及优化
人工智能·算法·自动曝光
思茂信息12 分钟前
CST案例:可调谐全硅手性超表面在太赫兹频段
网络·人工智能·算法·重构·cst·电磁仿真
天真小巫28 分钟前
六年之约-2026.5.21
职场和发展
呃呃本32 分钟前
算法题(动态规划)
算法·动态规划
pen-ai42 分钟前
Kennard-Stone (KS) 算法详解 —— 从实验设计到样本划分的经典方法
人工智能·算法·机器学习
开压路机1 小时前
数据结构:图
数据结构·算法
小白|1 小时前
cann-learning-hub:昇腾CANN社区学习中心完全指南
java·c++·算法
kobesdu1 小时前
当算法跑不通时:3D激光SLAM工程实践中的隐藏陷阱与全链路排查
算法·3d
金创想1 小时前
积木移动题目分析及解题思路——木块问题(1)
c++·算法·字符串·c·刷题·信息学奥赛·积木