All problems
0583EasyMathSimulation

Canteen Refills on the Ridge

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1518Water Bottles

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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