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
相关推荐
疯狂的喵3 小时前
C++编译期多态实现
开发语言·c++·算法
scx201310043 小时前
20260129LCA总结
算法·深度优先·图论
2301_765703143 小时前
C++中的协程编程
开发语言·c++·算法
m0_748708053 小时前
实时数据压缩库
开发语言·c++·算法
小魏每天都学习3 小时前
【算法——c/c++]
c语言·c++·算法
智码未来学堂4 小时前
探秘 C 语言算法之枚举:解锁解题新思路
c语言·数据结构·算法
Halo_tjn4 小时前
基于封装的专项 知识点
java·前端·python·算法
春日见4 小时前
如何避免代码冲突,拉取分支
linux·人工智能·算法·机器学习·自动驾驶
副露のmagic4 小时前
更弱智的算法学习 day59
算法
u0109272715 小时前
C++中的RAII技术深入
开发语言·c++·算法