Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ac42688

Browse files
committed
Add solution #3100
1 parent bd03519 commit ac42688

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,6 +2169,7 @@
21692169
3095|[Shortest Subarray With OR at Least K I](./solutions/3095-shortest-subarray-with-or-at-least-k-i.js)|Easy|
21702170
3097|[Shortest Subarray With OR at Least K II](./solutions/3097-shortest-subarray-with-or-at-least-k-ii.js)|Medium|
21712171
3099|[Harshad Number](./solutions/3099-harshad-number.js)|Easy|
2172+
3100|[Water Bottles II](./solutions/3100-water-bottles-ii.js)|Medium|
21722173
3105|[Longest Strictly Increasing or Strictly Decreasing Subarray](./solutions/3105-longest-strictly-increasing-or-strictly-decreasing-subarray.js)|Easy|
21732174
3108|[Minimum Cost Walk in Weighted Graph](./solutions/3108-minimum-cost-walk-in-weighted-graph.js)|Hard|
21742175
3110|[Score of a String](./solutions/3110-score-of-a-string.js)|Easy|

solutions/3100-water-bottles-ii.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* 3100. Water Bottles II
3+
* https://leetcode.com/problems/water-bottles-ii/
4+
* Difficulty: Medium
5+
*
6+
* You are given two integers numBottles and numExchange.
7+
*
8+
* numBottles represents the number of full water bottles that you initially have. In one
9+
* operation, you can perform one of the following operations:
10+
* - Drink any number of full water bottles turning them into empty bottles.
11+
* - Exchange numExchange empty bottles with one full water bottle. Then, increase
12+
* numExchange by one.
13+
*
14+
* Note that you cannot exchange multiple batches of empty bottles for the same value of
15+
* numExchange. For example, if numBottles == 3 and numExchange == 1, you cannot exchange
16+
* 3 empty water bottles for 3 full bottles.
17+
*
18+
* Return the maximum number of water bottles you can drink.
19+
*/
20+
21+
/**
22+
* @param {number} numBottles
23+
* @param {number} numExchange
24+
* @return {number}
25+
*/
26+
var maxBottlesDrunk = function(numBottles, numExchange) {
27+
let result = 0;
28+
let full = numBottles;
29+
let empty = 0;
30+
31+
while (full > 0) {
32+
result += full;
33+
empty += full;
34+
full = 0;
35+
if (empty >= numExchange) {
36+
full = 1;
37+
empty -= numExchange;
38+
numExchange++;
39+
}
40+
}
41+
42+
return result;
43+
};

0 commit comments

Comments
 (0)