#3100
Medium Algorithms Water bottles ii
Math Simulation
78.2% acceptance
Feb 25, 2026
534
102
You are given two integers numBottles and numExchange.
numBottles represents the number of full water bottles that you initially have. In one operation, you can perform one of the following operations:
Drink any number of full water bottles turning them into empty bottles.
Exchange numExchange empty bottles with one full water bottle. Then, increase numExchange by one.
Note that you cannot exchange multiple batches of empty bottles for the same value of numExchange.
Return the maximum number of water bottles you can drink.
Solution
Rust
Time O(n²)
Space O(1)
impl Solution {
pub fn max_bottles_drunk(mut num_bottles: i32, mut num_exchange: i32) -> i32 {
let mut drunk = 0;
let mut empty = 0;
while num_bottles > 0 || empty >= num_exchange {
drunk += num_bottles;
empty += num_bottles;
num_bottles = 0;
while empty >= num_exchange {
empty -= num_exchange;
num_exchange += 1;
num_bottles += 1;
}
}
drunk
}
}