Leetcode 1518. Water Bottles

Problem

There are numBottles water bottles that are initially full of water. You can exchange numExchange empty water bottles from the market with one full water bottle.

The operation of drinking a full water bottle turns it into an empty bottle.

Given the two integers numBottles and numExchange, return the maximum number of water bottles you can drink.

Algorithm

The process involves repeatedly exchanging empty bottles for new drinks, drinking them, and then continuing to exchange the newly obtained empty bottles. This cycle repeats until the number of empty bottles is insufficient for further exchanges.

Code

python3 复制代码
class Solution:
    def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
        ans, blank = 0, 0
        while numBottles or blank >= numExchange:
            ans += numBottles
            blank += numBottles
            numBottles = blank // numExchange
            blank %= numExchange
        
        return ans
相关推荐
C雨后彩虹4 小时前
任务最优调度
java·数据结构·算法·华为·面试
少林码僧6 小时前
2.31 机器学习神器项目实战:如何在真实项目中应用XGBoost等算法
人工智能·python·算法·机器学习·ai·数据挖掘
钱彬 (Qian Bin)6 小时前
项目实践15—全球证件智能识别系统(切换为Qwen3-VL-8B-Instruct图文多模态大模型)
人工智能·算法·机器学习·多模态·全球证件识别
Niuguangshuo7 小时前
EM算法详解:解密“鸡生蛋“的机器学习困局
算法·机器学习·概率论
a3158238067 小时前
Android 大图显示策略优化显示(一)
android·算法·图片加载·大图片
一条大祥脚7 小时前
26.1.9 轮廓线dp 状压最短路 构造
数据结构·c++·算法
鲨莎分不晴7 小时前
反向传播的数学本质:链式法则与动态规划的完美共舞
算法·动态规划
sonadorje8 小时前
逻辑回归中的条件概率
算法·机器学习·逻辑回归
cici158748 小时前
基于Pan-Tompkins算法的ECG信号HRV提取方案
算法
McGrady-1758 小时前
拓扑导航 vs 几何导航的具体实现位置
算法