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
相关推荐
九年义务漏网鲨鱼17 分钟前
【多模态大模型面经】现代大模型架构(一): 组注意力机制(GQA)和 RMSNorm
人工智能·深度学习·算法·架构·大模型·强化学习
闲人编程25 分钟前
CPython与PyPy性能对比:不同解释器的优劣分析
python·算法·编译器·jit·cpython·codecapsule
杜子不疼.32 分钟前
【C++】深入解析AVL树:平衡搜索树的核心概念与实现
android·c++·算法
小武~34 分钟前
Leetcode 每日一题C 语言版 -- 88 merge sorted array
c语言·算法·leetcode
e***U82037 分钟前
算法设计模式
算法·设计模式
徐子童2 小时前
数据结构----排序算法
java·数据结构·算法·排序算法·面试题
hansang_IR2 小时前
【记录】四道双指针
c++·算法·贪心·双指针
_OP_CHEN2 小时前
算法基础篇:(十二)基础算法之倍增思想:从快速幂到大数据运算优化
大数据·c++·算法·acm·算法竞赛·倍增思想
CoovallyAIHub2 小时前
分割万事万物的AI,再进化!Meta SAM 3 来了,支持中文提示词!
深度学习·算法·计算机视觉
九年义务漏网鲨鱼2 小时前
蓝桥杯算法——记忆化搜索
算法·职场和发展·蓝桥杯