1
class Solution {
2
public int minimumRefill(int[] p, int ca, int cb) {
3

4
int refill = 0,
5
oca = ca,
6
ocb = cb; // let save our orginal capacity , needed to refill can again
7
int i = 0, j = p.length - 1; // starting both end
8

9
while (i <= j) {
10

11
if (i == j) { // mean both at same position
12
if (ca >= cb) {
13
if (p[i] > ca) {
14
refill++;
15
}
16
} else {
17
if (p[j] > cb) {
18
refill++;
19
}
20
}
21
// no more plant left for watering so break loop
22
break;
23
}
24

25
// first check if they have sufficient amount of water
26
// if not then refill it with orginal capacity
27

28
if (p[i] > ca) {
29
refill++;
30
ca = oca;
31
}
32
if (p[j] > cb) {
33
refill++;
34
cb = ocb;
35
}
36

37
// decrease consumed water
38
ca -= p[i];
39
cb -= p[j];
40

41
// move both
42
i++;
43
j--;
44
}
45
return refill;
46
}
47
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0