Skip to content

Commit eb9ab59

Browse files
Merge pull request #309 from tiennguyen2310/patch-93
Create WaterBottles.java
2 parents ddccd10 + cf3b6be commit eb9ab59

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
1518. Water Bottles
3+
Solved
4+
Easy
5+
Topics
6+
premium lock icon
7+
Companies
8+
Hint
9+
There are numBottles water bottles that are initially full of water. You can exchange numExchange empty water bottles from the market with one full water bottle.
10+
11+
The operation of drinking a full water bottle turns it into an empty bottle.
12+
13+
Given the two integers numBottles and numExchange, return the maximum number of water bottles you can drink.
14+
15+
16+
17+
Example 1:
18+
19+
20+
Input: numBottles = 9, numExchange = 3
21+
Output: 13
22+
Explanation: You can exchange 3 empty bottles to get 1 full water bottle.
23+
Number of water bottles you can drink: 9 + 3 + 1 = 13.
24+
Example 2:
25+
26+
27+
Input: numBottles = 15, numExchange = 4
28+
Output: 19
29+
Explanation: You can exchange 4 empty bottles to get 1 full water bottle.
30+
Number of water bottles you can drink: 15 + 3 + 1 = 19.
31+
32+
33+
Constraints:
34+
35+
1 <= numBottles <= 100
36+
2 <= numExchange <= 100
37+
*/
38+
class Solution {
39+
public int numWaterBottles(int numBottles, int numExchange) {
40+
int res = numBottles;
41+
while(numBottles >= numExchange){
42+
// turns into full
43+
res += (numBottles / numExchange);
44+
numBottles = (numBottles % numExchange) + (numBottles / numExchange);
45+
}
46+
return res;
47+
}
48+
}

0 commit comments

Comments
 (0)