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 小时前
图结构完全解析:从基础概念到遍历实现
javascript·后端·算法
m0_736919103 小时前
C++代码风格检查工具
开发语言·c++·算法
yugi9878383 小时前
基于MATLAB强化学习的单智能体与多智能体路径规划算法
算法·matlab
DuHz4 小时前
超宽带脉冲无线电(Ultra Wideband Impulse Radio, UWB)简介
论文阅读·算法·汽车·信息与通信·信号处理
Polaris北极星少女4 小时前
TRSV优化2
算法
代码游侠5 小时前
C语言核心概念复习——网络协议与TCP/IP
linux·运维·服务器·网络·算法
2301_763472465 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
abluckyboy6 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法
园小异6 小时前
2026年技术面试完全指南:从算法到系统设计的实战突破
算法·面试·职场和发展
m0_706653236 小时前
分布式系统安全通信
开发语言·c++·算法