Understand Water Bottles 2 Problem

Water Bottles II

Medium


Hint

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:
  1. Drink any number of full water bottles turning them into empty bottles.
  2. Exchange numExchange empty bottles with one full water bottle. Then, increase numExchange by one.

⚠️ Note: You cannot exchange multiple batches of empty bottles for the same value of numExchange.
For example, if numBottles == 3 and numExchange == 1, you cannot exchange 3 empty water bottles for 3 full bottles.

Return the maximum number of water bottles you can drink.


Example 1

Input:

numBottles = 13, numExchange = 6

Output:

15

Step-by-step table (Full / Empty / numExchange / cumulative drunk):

StepActionFull (after)Empty (after)numExchange (used → new / or current)Drunk (cumulative)
0Start13060
1Drink 13013613
2Exchange (use 6)176 → 713
3Exchange (use 7)207 → 813
4Drink 202815

Final answer: 15


Example 2

Input:

numBottles = 10, numExchange = 3

Output:

13

Step-by-step table (Full / Empty / numExchange / cumulative drunk):

StepActionFull (after)Empty (after)numExchange (used → new / or current)Drunk (cumulative)
0Start10030
1Drink 10010310
2Exchange (use 3)173 → 410
3Exchange (use 4)234 → 510
4Drink 205512
5Exchange (use 5)105 → 612
6Drink 101613

Final answer: 13


Constraints

  • 1 <= numBottles <= 100
  • 1 <= numExchange <= 100
Category:
  • Maths
  • Simulation
  • Leetcode Problem of the Day
Programming Language:
  • Java
Reference Link:

https://leetcode.com/problems/water-bottles-ii/description/

Java
Output:

Loading component...

Loading component...