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语言程序设计】第39篇:预处理器与宏定义
c语言·开发语言·c++·vscode·算法·visual studio code·visual studio
参.商.3 分钟前
【Day43】49. 字母异位词分组
leetcode·golang
m0_5698814711 分钟前
C++中的装饰器模式变体
开发语言·c++·算法
笒鬼鬼13 分钟前
【API接口】最新可用红果短剧接口
算法·api·笒鬼鬼·红果短剧·接口源码
weixin_4219226914 分钟前
C++与边缘计算
开发语言·c++·算法
2401_8319207416 分钟前
C++编译期数组操作
开发语言·c++·算法
殷紫川23 分钟前
秒杀系统高并发核心优化与落地全指南
算法·架构
野犬寒鸦35 分钟前
JVM垃圾回收机制面试常问问题及详解
java·服务器·开发语言·jvm·后端·算法·面试
风酥糖41 分钟前
Godot游戏练习01-第16节-游戏中的状态机
算法·游戏·godot
参.商.43 分钟前
【Day45】647. 回文子串 5. 最长回文子串
leetcode·golang