We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Water Bottles II

Number: 3336

Difficulty: Medium

Paid? No

Companies: Microsoft


Problem Description

Given two integers numBottles and numExchange, you start with numBottles full water bottles. You can perform the following operations:

  1. Drink any number of full water bottles to turn them into empty bottles.
  2. Exchange exactly numExchange empty bottles for one full water bottle. After an exchange, increase numExchange by one (i.e., the exchange rate increases by one for each exchange performed).

Return the maximum number of water bottles you can drink.


Key Insights

  • Always drink all available full bottles to maximize the number of empty bottles you can reuse.
  • Each exchange uses exactly numExchange empty bottles to obtain one full bottle, but the exchange rate increases after every exchange.
  • Simulation is feasible given the input constraints (both numBottles and numExchange are at most 100).
  • Update the count of empty bottles after each exchange, accounting for both the remaining empty bottles and the new empties from the newly drunk bottles.

Space and Time Complexity

Time Complexity: O(n) where n is the number of exchanges (bounded by small constants due to constraints). Space Complexity: O(1)


Solution

We simulate the process with the following steps:

  1. Initialize total_drunk with numBottles since you start by drinking all the bottles, and initialize empty_bottles as numBottles.
  2. Use a loop to simulate the exchange process as long as empty_bottles is at least as large as the current exchange requirement (current_exchange). a. Compute new_bottles = empty_bottles // current_exchange (full bottles you obtain). b. Increment total_drunk with new_bottles. c. Update empty_bottles = (empty_bottles % current_exchange) + new_bottles. d. Increase current_exchange by new_bottles (each exchange increases the requirement by one).
  3. The loop terminates once you cannot perform another exchange, and total_drunk holds the maximum water bottles consumed.

This simulation uses a while loop with constant space and operations that are O(1) per iteration.


Code Solutions

# Python implementation of the solution

def maxBottles(numBottles: int, numExchange: int) -> int:
    # Initially drink all water bottles
    total_drunk = numBottles
    empty_bottles = numBottles
    current_exchange = numExchange

    # Continue exchanging as long as we have enough empty bottles
    while empty_bottles >= current_exchange:
        # Determine how many full bottles can be obtained from empty bottles
        new_bottles = empty_bottles // current_exchange
        # Add the new bottles to our total count
        total_drunk += new_bottles
        # Update the empty bottles: remaining bottles plus the ones from drinking new bottles
        empty_bottles = (empty_bottles % current_exchange) + new_bottles
        # Update the exchange rate as each exchange increases the requirement by one for each new bottle
        current_exchange += new_bottles

    return total_drunk

# Example usage
print(maxBottles(13, 6))  # Expected output: 15
← Back to All Questions