LeetCode 题解工作台

换水问题 II

给你两个整数 numBottles 和 numExchange 。 numBottles 代表你最初拥有的满水瓶数量。在一次操作中,你可以执行以下操作之一: 喝掉任意数量的满水瓶,使它们变成空水瓶。 用 numExchange 个空水瓶交换一个满水瓶。然后,将 numExchange 的值增加 1 …

category

2

题型

code_blocks

8

代码语言

hub

3

相关题

当前训练重点

中等 · 数学·结合·模拟

bolt

答案摘要

我们可以在一开始就喝掉所有的满水瓶,因此初始时我们喝到的水数量为 。然后我们不断地进行以下操作: - 如果当前有 个空水瓶,我们就可以用它们换一瓶满水瓶,换完后, 的值增加 。然后,我们喝掉这瓶水,喝到的水数量增加 ,空水瓶数量增加 。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数学·结合·模拟 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你两个整数 numBottlesnumExchange

numBottles 代表你最初拥有的满水瓶数量。在一次操作中,你可以执行以下操作之一:

  • 喝掉任意数量的满水瓶,使它们变成空水瓶。
  • numExchange 个空水瓶交换一个满水瓶。然后,将 numExchange 的值增加 1 。

注意,你不能使用相同的 numExchange 值交换多批空水瓶。例如,如果 numBottles == 3 并且 numExchange == 1 ,则不能用 3 个空水瓶交换成 3 个满水瓶。

返回你 最多 可以喝到多少瓶水。

 

示例 1:

输入:numBottles = 13, numExchange = 6
输出:15
解释:上表显示了满水瓶的数量、空水瓶的数量、numExchange 的值,以及累计喝掉的水瓶数量。

示例 2:

输入:numBottles = 10, numExchange = 3
输出:13
解释:上表显示了满水瓶的数量、空水瓶的数量、numExchange 的值,以及累计喝掉的水瓶数量。

 

提示:

  • 1 <= numBottles <= 100
  • 1 <= numExchange <= 100
lightbulb

解题思路

方法一:模拟

我们可以在一开始就喝掉所有的满水瓶,因此初始时我们喝到的水数量为 numBottles\textit{numBottles}。然后我们不断地进行以下操作:

  • 如果当前有 numExchange\textit{numExchange} 个空水瓶,我们就可以用它们换一瓶满水瓶,换完后,numExchange\textit{numExchange} 的值增加 11。然后,我们喝掉这瓶水,喝到的水数量增加 11,空水瓶数量增加 11
  • 如果当前没有 numExchange\textit{numExchange} 个空水瓶,那么我们就不能再换水了,此时我们就可以停止操作。

我们不断地进行上述操作,直到我们无法再换水为止。最终我们喝到的水的数量就是答案。

时间复杂度 O(n)O(\sqrt{n}),其中 nn 是初始的满水瓶数量。空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
class Solution:
    def maxBottlesDrunk(self, numBottles: int, numExchange: int) -> int:
        ans = numBottles
        while numBottles >= numExchange:
            numBottles -= numExchange
            numExchange += 1
            ans += 1
            numBottles += 1
        return ans
speed

复杂度分析

指标
时间complexity is O(log(numBottles)) if tracking exchanges iteratively, since each exchange reduces empty bottles. Space complexity is O(1) because only counters for full and empty bottles are maintained.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Focus on simulating each exchange step carefully rather than jumping to a formula.

  • question_mark

    Check whether edge cases like numBottles < numExchange are handled correctly.

  • question_mark

    Watch for infinite loops if empty bottles are not reduced after exchanges.

warning

常见陷阱

外企场景
  • error

    Trying to combine multiple exchanges at once instead of following the iterative pattern.

  • error

    Forgetting to increment the total drink count after each new full bottle obtained.

  • error

    Neglecting the scenario when remaining empty bottles are less than numExchange, causing incorrect loops.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Change numExchange to vary at each step, testing dynamic exchange rates.

  • arrow_right_alt

    Introduce a limit on total exchanges allowed, requiring careful simulation.

  • arrow_right_alt

    Start with zero full bottles but some empty bottles to see if exchange logic triggers correctly.

help

常见问题

外企场景

换水问题 II题解:数学·结合·模拟 | LeetCode #3100 中等