Skip to main content
Back to problems
#1518
Easy Algorithms

Water bottles

Math Simulation
72.6% acceptance
Feb 25, 2026
2264
177
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn num_water_bottles(mut num_bottles: i32, num_exchange: i32) -> i32 {
    let mut drunk = 0;
    let mut empty = 0;
    while num_bottles > 0 {
      drunk += num_bottles;
      empty += num_bottles;
      num_bottles = empty / num_exchange;
      empty %= num_exchange;
    }
    drunk
  }
}