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
| class Solution { public int minimumRefill(int[] plants, int capacityA, int capacityB) { int times = 0, waterA = capacityA, waterB = capacityB;
int i = 0, j = plants.length - 1; while (i < j) { if (waterA < plants[i]) { times++; waterA = capacityA; } waterA -= plants[i++]; if (waterB < plants[j]) { times++; waterB = capacityB; } waterB -= plants[j--]; }
if (i == j && Math.max(waterA, waterB) < plants[j]) { times++; }
return times; } }
|