File tree 1 file changed +12
-11
lines changed
scripts/algorithms/W/Watering Plants II
1 file changed +12
-11
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 136 ms (Top 56.52%) | Memory: 53.8 MB (Top 73.91%)
1
2
var minimumRefill = function ( plants , capacityA , capacityB ) {
2
3
const n = plants . length ;
3
-
4
+
4
5
let left = 0 ;
5
6
let right = n - 1 ;
6
-
7
+
7
8
let remA = capacityA ;
8
9
let remB = capacityB ;
9
-
10
+
10
11
let refills = 0 ;
11
-
12
+
12
13
while ( left < right ) {
13
14
const leftAmount = plants [ left ++ ] ;
14
15
const rightAmount = plants [ right -- ] ;
15
-
16
+
16
17
if ( leftAmount > remA ) {
17
18
++ refills ;
18
19
remA = capacityA ;
19
20
}
20
21
remA -= leftAmount ;
21
-
22
+
22
23
if ( rightAmount > remB ) {
23
24
++ refills ;
24
25
remB = capacityB ;
25
26
}
26
27
remB -= rightAmount ;
27
-
28
+
28
29
}
29
-
30
+
30
31
if ( left === right ) {
31
32
const midAmount = plants [ left ] ;
32
-
33
+
33
34
if ( remB > remA ) {
34
35
if ( remB < midAmount ) ++ refills ;
35
36
}
36
37
else {
37
38
if ( remA < midAmount ) ++ refills ;
38
39
}
39
40
}
40
-
41
+
41
42
return refills ;
42
- } ;
43
+ } ;
You can’t perform that action at this time.
0 commit comments