-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1518-water-bottles.js
More file actions
31 lines (28 loc) · 896 Bytes
/
1518-water-bottles.js
File metadata and controls
31 lines (28 loc) · 896 Bytes
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
/**
* 1518. Water Bottles
* https://leetcode.com/problems/water-bottles/
* Difficulty: Easy
*
* 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.
*
* The operation of drinking a full water bottle turns it into an empty bottle.
*
* Given the two integers numBottles and numExchange, return the maximum number of water bottles you
* can drink.
*/
/**
* @param {number} numBottles
* @param {number} numExchange
* @return {number}
*/
var numWaterBottles = function(numBottles, numExchange) {
let result = numBottles;
let emptyBottles = numBottles;
while (emptyBottles >= numExchange) {
const newBottles = Math.floor(emptyBottles / numExchange);
result += newBottles;
emptyBottles = newBottles + (emptyBottles % numExchange);
}
return result;
};