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
相关推荐
北诺南兮4 小时前
大模型算法面试笔记——多头潜在注意力(MLA)
笔记·深度学习·算法
微知语4 小时前
悬垂引用的攻防战:Rust 如何从根源杜绝内存访问灾难
开发语言·算法·rust
MrZhangBaby5 小时前
SQL-leetcode—3475. DNA 模式识别
数据库·sql·leetcode
_张一凡5 小时前
【AIGC面试面经第四期】LLM-Qwen相关问答
面试·职场和发展·aigc
万能的小裴同学5 小时前
C++ 鸭科夫手柄适配
开发语言·c++·算法
得物技术5 小时前
RAG—Chunking策略实战|得物技术
数据库·人工智能·算法
gugugu.7 小时前
算法:滑动窗口类型题目的总结
算法·哈希算法
大数据张老师7 小时前
数据结构——直接插入排序
数据结构·算法·排序算法·1024程序员节
hoiii1877 小时前
基于SVM与HOG特征的交通标志检测与识别
算法·机器学习·支持向量机