Watering Plants
给一个数组, 里面是数字是灌溉i位上的植物需要的水, 给一个整数, 代表水桶的大小, 每次走一步, 没水需要回原点取水, 求一共多少步.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Solution { public int wateringPlants(int[] plants, int capacity) { int res = 0; int c = capacity; for(int i = 0; i < plants.length; i++){ if(plants[i] > c){ res += (i + (i + 1)); c = capacity; c -= plants[i]; } else{ res++; c -= plants[i]; } } return res; } } |