Skip to content

Commit df9998d

Browse files
authored
Create 2105-watering-plants-ii.js
1 parent b916321 commit df9998d

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

2105-watering-plants-ii.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {number[]} plants
3+
* @param {number} capacityA
4+
* @param {number} capacityB
5+
* @return {number}
6+
*/
7+
const minimumRefill = function(plants, capacityA, capacityB) {
8+
const n = plants.length
9+
let [left, right] = [0, n - 1]
10+
let [A, B] = [capacityA, capacityB]
11+
let ans = 0
12+
while (left < right) {
13+
if (A < plants[left]) {
14+
A = capacityA
15+
ans += 1
16+
}
17+
18+
A -= plants[left]
19+
left += 1
20+
if (B < plants[right]) {
21+
B = capacityB
22+
ans += 1
23+
}
24+
25+
B -= plants[right]
26+
right -= 1
27+
}
28+
29+
30+
if (left != right || A >= plants[left] || B >= plants[left]) return ans
31+
return ans + 1
32+
};
33+

0 commit comments

Comments
 (0)