forked from SjxSubham/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1518. Water Bottles.cpp
More file actions
45 lines (39 loc) · 1.41 KB
/
1518. Water Bottles.cpp
File metadata and controls
45 lines (39 loc) · 1.41 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
37
38
39
40
41
42
43
44
45
class Solution {
public:
int numWaterBottles(int numBottles, int numExchange) {
int totalDrunk = numBottles;
int emptyBottles = numBottles;
while (emptyBottles >= numExchange) {
int newBottles = emptyBottles / numExchange;
totalDrunk += newBottles;
emptyBottles = emptyBottles % numExchange + newBottles;
}
return totalDrunk;
}
};
/*
Alternative Mathematical Solution (More Efficient):
class Solution {
public:
int numWaterBottles(int numBottles, int numExchange) {
// Mathematical formula: numBottles + (numBottles - 1) / (numExchange - 1)
return numBottles + (numBottles - 1) / (numExchange - 1);
}
};
Problem Explanation:
- You have numBottles full water bottles
- After drinking, you get numBottles empty bottles
- You can exchange numExchange empty bottles for 1 new full bottle
- Find maximum bottles you can drink
Example: numBottles = 9, numExchange = 3
- Drink 9 bottles → 9 empty bottles
- Exchange 9 empty → 3 new bottles, 0 empty remaining
- Drink 3 bottles → 3 empty bottles
- Exchange 3 empty → 1 new bottle, 0 empty remaining
- Drink 1 bottle → 1 empty bottle
- Cannot exchange anymore (need 3 empty for 1 new)
- Total drunk: 9 + 3 + 1 = 13
Time Complexity: O(log(numBottles)) - simulation approach
O(1) - mathematical approach
Space Complexity: O(1)
*/