Skip to content

Commit 2554fdf

Browse files
committed
Add solution #1518
1 parent 7acc42d commit 2554fdf

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,342 LeetCode solutions in JavaScript
1+
# 1,343 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1159,6 +1159,7 @@
11591159
1513|[Number of Substrings With Only 1s](./solutions/1513-number-of-substrings-with-only-1s.js)|Medium|
11601160
1514|[Path with Maximum Probability](./solutions/1514-path-with-maximum-probability.js)|Medium|
11611161
1515|[Best Position for a Service Centre](./solutions/1515-best-position-for-a-service-centre.js)|Hard|
1162+
1518|[Water Bottles](./solutions/1518-water-bottles.js)|Easy|
11621163
1519|[Number of Nodes in the Sub-Tree With the Same Label](./solutions/1519-number-of-nodes-in-the-sub-tree-with-the-same-label.js)|Medium|
11631164
1524|[Number of Sub-arrays With Odd Sum](./solutions/1524-number-of-sub-arrays-with-odd-sum.js)|Medium|
11641165
1528|[Shuffle String](./solutions/1528-shuffle-string.js)|Easy|

solutions/1518-water-bottles.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 1518. Water Bottles
3+
* https://leetcode.com/problems/water-bottles/
4+
* Difficulty: Easy
5+
*
6+
* There are numBottles water bottles that are initially full of water. You can exchange numExchange
7+
* empty water bottles from the market with one full water bottle.
8+
*
9+
* The operation of drinking a full water bottle turns it into an empty bottle.
10+
*
11+
* Given the two integers numBottles and numExchange, return the maximum number of water bottles you
12+
* can drink.
13+
*/
14+
15+
/**
16+
* @param {number} numBottles
17+
* @param {number} numExchange
18+
* @return {number}
19+
*/
20+
var numWaterBottles = function(numBottles, numExchange) {
21+
let result = numBottles;
22+
let emptyBottles = numBottles;
23+
24+
while (emptyBottles >= numExchange) {
25+
const newBottles = Math.floor(emptyBottles / numExchange);
26+
result += newBottles;
27+
emptyBottles = newBottles + (emptyBottles % numExchange);
28+
}
29+
30+
return result;
31+
};

0 commit comments

Comments
 (0)