-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet-code-q-35.java
More file actions
36 lines (28 loc) · 1.32 KB
/
leet-code-q-35.java
File metadata and controls
36 lines (28 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
/**
* Calculates the maximum number of water bottles that can be consumed.
*
* @param numBottles The initial number of full water bottles.
* @param numExchange The number of empty bottles needed for one full bottle.
* @return The total number of bottles drunk.
*/
public int numWaterBottles(int numBottles, int numExchange) {
// Start by drinking all the initial bottles.
int totalDrunk = numBottles;
// These initial bottles are now empty.
int emptyBottles = numBottles;
// Loop as long as we can exchange empty bottles for full ones.
while (emptyBottles >= numExchange) {
// Determine how many new full bottles we can get.
int newBottles = emptyBottles / numExchange;
// Find the leftover empty bottles after the exchange.
int leftoverBottles = emptyBottles % numExchange;
// Add the newly acquired bottles to our total count.
totalDrunk += newBottles;
// Update our count of empty bottles. It consists of the leftovers
// plus the new bottles we just drank.
emptyBottles = leftoverBottles + newBottles;
}
return totalDrunk;
}
}