You want to water n plants in your garden with a watering can. The plants are arranged in a row and are labeled from 0 to n - 1 from left to right where the ith plant is located at x = i. There is a river at x = -1 that you can refill your watering can at.
Each plant needs a specific amount of water. You will water the plants in the following way:
Water the plants in order from left to right.
After watering the current plant, if you do not have enough water to completely water the next plant, return to the river to fully refill the watering can.
You cannot refill the watering can early.
You are initially at the river (i.e., x = -1). It takes one step to move
one unit on the x-axis.
Given a 0-indexed integer array plants of n integers, where
plants[i] is the amount of water the ith plant needs, and an integer
capacity representing the watering can capacity, return thenumber of steps needed to water all the plants.
Input: plants =[2,2,3,3], capacity =5Output: 14Explanation: Start at the river with a full watering can:- Walk to plant 0(1 step) and water it. Watering can has 3 units of water.- Walk to plant 1(1 step) and water it. Watering can has 1 unit of water.- Since you cannot completely water plant 2, walk back to the river to refill(2 steps).- Walk to plant 2(3 steps) and water it. Watering can has 2 units of water.- Since you cannot completely water plant 3, walk back to the river to refill(3 steps).- Walk to plant 3(4 steps) and water it.Steps needed =1+1+2+3+3+4=14.
Input: plants =[1,1,1,4,2,3], capacity =4Output: 30Explanation: Start at the river with a full watering can:- Water plants 0,1, and 2(3 steps). Return to river(3 steps).- Water plant 3(4 steps). Return to river(4 steps).- Water plant 4(5 steps). Return to river(5 steps).- Water plant 5(6 steps).Steps needed =3+3+4+4+5+5+6=30.
Input: plants =[7,7,7,7,7,7,7], capacity =8Output: 49Explanation: You have to refill before watering each plant.Steps needed =1+1+2+2+3+3+4+4+5+5+6+6+7=49.
We simulate the process step by step, always walking to the next plant, watering it, and refilling only when necessary. The number of steps is the sum of all moves, including returns to the river.
classSolution {
public:int wateringPlants(vector<int>& plants, int capacity) {
int ans =0, curr = capacity;
for (int i =0; i < plants.size(); ++i) {
if (curr < plants[i]) {
ans +=2* i;
curr = capacity;
}
ans++;
curr -= plants[i];
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
funcwateringPlants(plants []int, capacityint) int {
ans, curr:=0, capacityfori, v:=rangeplants {
ifcurr < v {
ans+=2*icurr = capacity }
ans++curr-=v }
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
publicintwateringPlants(int[] plants, int capacity) {
int ans = 0, curr = capacity;
for (int i = 0; i < plants.length; i++) {
if (curr < plants[i]) {
ans += 2 * i;
curr = capacity;
}
ans++;
curr -= plants[i];
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
funwateringPlants(plants: IntArray, capacity: Int): Int {
var ans = 0var curr = capacity
for (i in plants.indices) {
if (curr < plants[i]) {
ans +=2 * i
curr = capacity
}
ans++ curr -= plants[i]
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
from typing import List
classSolution:
defwateringPlants(self, plants: List[int], capacity: int) -> int:
ans =0 curr = capacity
for i, v in enumerate(plants):
if curr < v:
ans +=2* i
curr = capacity
ans +=1 curr -= v
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
impl Solution {
pubfnwatering_plants(plants: Vec<i32>, capacity: i32) -> i32 {
letmut ans =0;
letmut curr = capacity;
for (i, &v) in plants.iter().enumerate() {
if curr < v {
ans +=2* i asi32;
curr = capacity;
}
ans +=1;
curr -= v;
}
ans
}
}