[LeetCode] Water Bottles II

3100. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int maxBottlesDrunk(int numBottles, int numExchange) {
int res = numBottles, e = numBottles, b = 0;
while(e >= numExchange) {
e -= numExchange;
numExchange++;
e += 1;
res += 1;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/03/31/PS/LeetCode/water-bottles-ii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.