题目:
解题思路:
面对每一株植物有两种情况水够 or 水不够:
-
水够: result加1即向前走一步
-
水不够: 走回河边再走回来并向前走一步,走到下一植物 result += 2 * i + 1
int wateringPlants(int* plants, int plantsSize, int capacity) {
int result = 0;
int water = capacity;
for (int i = 0; i < plantsSize; i++) {
water -= plants[i];
if (water >= 0) {
result++;
}
else {
result += 2 * i + 1;
water = capacity - plants[i];
}
}
return result;
}