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
相关推荐
程序员-King.1 小时前
day158—回溯—全排列(LeetCode-46)
算法·leetcode·深度优先·回溯·递归
月挽清风2 小时前
代码随想录第七天:
数据结构·c++·算法
小O的算法实验室2 小时前
2026年AEI SCI1区TOP,基于改进 IRRT*-D* 算法的森林火灾救援场景下直升机轨迹规划,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
小郭团队3 小时前
2_1_七段式SVPWM (经典算法)算法理论与 MATLAB 实现详解
嵌入式硬件·算法·硬件架构·arm·dsp开发
充值修改昵称3 小时前
数据结构基础:从二叉树到多叉树数据结构进阶
数据结构·python·算法
Deepoch3 小时前
Deepoc数学大模型:发动机行业的算法引擎
人工智能·算法·机器人·发动机·deepoc·发动机行业
浅念-4 小时前
C语言小知识——指针(3)
c语言·开发语言·c++·经验分享·笔记·学习·算法
Hcoco_me4 小时前
大模型面试题84:是否了解 OpenAI 提出的Clip,它和SigLip有什么区别?为什么SigLip效果更好?
人工智能·算法·机器学习·chatgpt·机器人
BHXDML5 小时前
第九章:EM 算法
人工智能·算法·机器学习
却道天凉_好个秋5 小时前
目标检测算法与原理(三):PyTorch实现迁移学习
pytorch·算法·目标检测