Show problems as
Classic problem #1518EasyMathSimulation

Water Bottles

Real-world version: Canteen Refills on the Ridge
Tracked in this browser only
Read Water Bottles on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A ridge depot keeps hikers supplied with drinking water. You set out holding numBottles sealed bottles.

Emptying a sealed bottle leaves you with one used container, and you always keep your used containers. The depot will hand over one fresh sealed bottle in return for numExchange used containers, and you may go back to the counter as often as you like while you still hold that many used containers.

Return how many bottles you empty in total.

Examples

Example 1

Input
numBottles = 12, numExchange = 4
Output
15

Emptying the 12 sealed bottles leaves 12 containers, which trade for 3 sealed bottles. Emptying those brings the tally to 15 and leaves 3 containers, too few for another trade.

Example 2

Input
numBottles = 6, numExchange = 100
Output
6

Six containers never reach the 100 the counter asks for, so the only bottles emptied are the six carried in.

Example 3

Input
numBottles = 100, numExchange = 2
Output
199

Trips to the counter bring back 50, then 25, then 12 with one container held over, then 6, 3, 2 and 1, and the tally reaches 199.

Constraints

  • 1 <= numBottles <= 100
  • 2 <= numExchange <= 100

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def num_water_bottles(numBottles: int, numExchange: int) -> int:
Java
public int numWaterBottles(int numBottles, int numExchange)