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
相关推荐
wabs6665 小时前
关于二叉树【力扣572.另一棵树的子树的思考】
数据结构·c++·算法·leetcode·二叉树
hanlin035 小时前
刷题笔记:力扣第41题-缺失的第一个正数
笔记·算法·leetcode
shehuiyuelaiyuehao6 小时前
算法50,分治归并,数组中的逆序对
java·算法
阳明山水6 小时前
校准分位数驱动智能库存决策
人工智能·深度学习·算法·机器学习·架构
别动我齐刘海7 小时前
ROS2 Jazzy + C++ 实战路线——进阶学习3
c++·人工智能·vscode·python·算法·机器学习·机器人
zhangfeng11338 小时前
《从“人工适配“到“智能生成“:KernelSwift 跨国产芯片算子迁移全栈方案解读》 —— 强调范式跃迁和跨硬件属性,适合偏架构分析的写法
人工智能·算法·华为·ai编程·npu
0+1118 小时前
算法 --滑动窗口
c++·算法·leetcode
天真小巫8 小时前
2026.9.19总结
职场和发展
reikocao8 小时前
大场景下 Hessian 特征值放大与退化检测
算法
hetao17338378 小时前
2026-09-18 hetao1733837 的刷题记录
c++·算法