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
相关推荐
长存祈月心2 小时前
Rust Option 与 Result深度解析
算法
杭州杭州杭州3 小时前
机器学习(3)---线性算法,决策树,神经网络,支持向量机
算法·决策树·机器学习
不语n4 小时前
快速排序(Quick Sort)详解与图解
数据结构·算法·排序算法·快速排序·双指针排序
电鱼智能的电小鱼5 小时前
基于电鱼 ARM 工控机的AI视频智能分析方案:让传统监控变得更聪明
网络·arm开发·人工智能·嵌入式硬件·算法·音视频
初学者,亦行者5 小时前
Rust性能优化:内存对齐与缓存友好实战
算法·rust
py有趣5 小时前
LeetCode算法学习之杨辉三角
学习·算法·leetcode
小白菜又菜5 小时前
Leetcode 3100. Water Bottles II
算法·leetcode·职场和发展
北诺南兮5 小时前
大模型算法面试笔记——多头潜在注意力(MLA)
笔记·深度学习·算法