3100. Water Bottles II LeetCode Solution

In this guide, you will get 3100. Water Bottles II LeetCode Solution with the best time and space complexity. The solution to Water Bottles II problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Water Bottles II solution in C++
  4. Water Bottles II solution in Java
  5. Water Bottles II solution in Python
  6. Additional Resources
3100. Water Bottles II LeetCode Solution image

Problem Statement of Water Bottles II

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:

Drink any number of full water bottles turning them into empty bottles.
Exchange numExchange empty bottles with one full water bottle. Then, increase numExchange by one.

Note that 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
Explanation: The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk.

Example 2:

Input: numBottles = 10, numExchange = 3
Output: 13
Explanation: The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk.

See also  3451. Find Invalid IP Addresses LeetCode Solution

Constraints:

1 <= numBottles <= 100
1 <= numExchange <= 100

Complexity Analysis

  • Time Complexity: O\left(\frac{\texttt{numBottles}}{\texttt{numExchange}}\right)
  • Space Complexity: O(1)

3100. Water Bottles II LeetCode Solution in C++

class Solution {
 public:
  int maxBottlesDrunk(int numBottles, int numExchange) {
    int ans = numBottles;

    while (numBottles >= numExchange) {
      numBottles = (numBottles - numExchange + 1);
      ++numExchange;
      ++ans;
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

3100. Water Bottles II LeetCode Solution in Java

class Solution {
  public int maxBottlesDrunk(int numBottles, int numExchange) {
    int ans = numBottles;

    while (numBottles >= numExchange) {
      numBottles = (numBottles - numExchange + 1);
      ++numExchange;
      ++ans;
    }

    return ans;
  }
}
// code provided by PROGIEZ

3100. Water Bottles II LeetCode Solution in Python

class Solution:
  def maxBottlesDrunk(self, numBottles: int, numExchange: int) -> int:
    ans = numBottles

    while numBottles >= numExchange:
      numBottles = numBottles - numExchange + 1
      numExchange += 1
      ans += 1

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.