当前位置: 首页 > news >正文

[LeetCode] 1518. Water Bottles

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.

Example 1:
Example 1
Input: numBottles = 9, numExchange = 3
Output: 13
Explanation: You can exchange 3 empty bottles to get 1 full water bottle.
Number of water bottles you can drink: 9 + 3 + 1 = 13.

Example 2:
Example 2
Input: numBottles = 15, numExchange = 4
Output: 19
Explanation: You can exchange 4 empty bottles to get 1 full water bottle.
Number of water bottles you can drink: 15 + 3 + 1 = 19.

Constraints:
1 <= numBottles <= 100
2 <= numExchange <= 100

换水问题。

超市正在促销,你可以用 numExchange 个空水瓶从超市兑换一瓶水。最开始,你一共购入了 numBottles 瓶水。

如果喝掉了水瓶中的水,那么水瓶就会变成空的。

给你两个整数 numBottles 和 numExchange ,返回你 最多 可以喝到多少瓶水。

思路

这是一道模拟题。题目说了每 numExchange 个空瓶可以换一瓶水,那么我们可以模拟这个过程,每喝掉 numExchange 瓶水,就去兑换一瓶水,直到 numBottles 喝光。注意兑换的水要加回 numBottles。

复杂度

时间O(n)
空间O(1)

代码

Java实现

class Solution {public int numWaterBottles(int numBottles, int numExchange) {int count = 0;while (numBottles >= numExchange) {count += numExchange;numBottles -= numExchange - 1;}return count + numBottles;}
}
http://www.jsqmd.com/news/6692/

相关文章:

  • 2025 年西安品质楼盘住宅推荐排行榜权威发布,精选优质楼盘推荐
  • Python国庆祝福 - 指南
  • 某商业银行项目管理建设演进报告 - 指南
  • 题解:P14073 [GESP202509 五级] 数字选取
  • 2025西安新房住宅推荐排行榜发布,房屋品质、周边配套、交通便利性多维度选择指南!
  • 华为造车“内战”!徐直军下场做“启境”,会比余承东五界更强?
  • 余承东的新职位传递了华为重大信息
  • 张雪峰的事儿,大有文章
  • 词(持续更新)语言的边界就是
  • Group Theory (I)
  • 财务分析怎么做 - 智慧园区
  • 技术内容思路构建Promot
  • Group Theory
  • 摩尔定律的历史与AI统计学:从命名误导到本质洞察
  • 立场客观性警告Prompt
  • # SICP学习笔记:计算机程序的构造与解释
  • 3.劝学
  • 2025包装机厂家推荐榜单出炉:拉伸膜真空包装机,全自动真空包装机,滚动式真空包装机,食品真空包装机,气调包装机公司推荐!
  • 2025年真空机厂家推荐榜:平台式真空封口机,拉伸膜真空覆膜机,全自动拉伸膜真空包装机,滚动连续式真空包装机,双面拉伸真空包装机公司实力甄选指南
  • 【半导体器件 | 笔记】金属氧化物半导体场效应晶体管(MOSFET)
  • 元人文AI场域:在有限与无限的纠缠中走向智慧文明
  • 【半导体器件 | 笔记】pn结二极管
  • 251001
  • Day2:Linux文件目录移到拷贝与vim编辑器使用指南
  • 【半导体物理 | 笔记】第八章 半导体表面与MIS结构
  • 【半导体物理 | 笔记】第四章 半导体的导电性
  • 【半导体物理 | 笔记】第五章 非平衡载流子
  • 【AHK】暗黑3助手,加强版鼠标宏
  • 【当前赛季】第36赛季:地狱魔王9月12日开启
  • 第36赛季:地狱魔王9月12日开启