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:
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 the number of steps needed to water all the plants.
Example 1:
Input: plants = [2,2,3,3], capacity = 5 Output: 14 Explanation: 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.
Example 2:
Input: plants = [1,1,1,4,2,3], capacity = 4 Output: 30 Explanation: 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.
Example 3:
Input: plants = [7,7,7,7,7,7,7], capacity = 8 Output: 49 Explanation: You have to refill before watering each plant. Steps needed = 1 + 1 + 2 + 2 + 3 + 3 + 4 + 4 + 5 + 5 + 6 + 6 + 7 = 49.
Constraints:
n == plants.length
1 <= n <= 1000
1 <= plants[i] <= 106
max(plants[i]) <= capacity <= 109
The problem involves calculating the minimum steps required to water n
plants arranged in a row, given a watering can with a specific capacity. We start at the river (position -1) and must water plants from left to right. If the water in the can is insufficient to water the next plant, we must return to the river to refill.
Approach:
The optimal approach is a simulation. We iterate through the plants, keeping track of the remaining water in the can. For each plant:
Check Water Level: If the current water level is sufficient to water the plant (water >= plants[i]
), we subtract the plant's water requirement from the current water level and increment the step count by 1 (for watering the plant).
Refill: If the water level is insufficient, we must return to the river and refill. The number of steps to do this is calculated as follows:
i * 2 + 1
: i
steps to return to the river, i
steps to get back to the current plant, and 1 step to water the plant.water = capacity - plants[i]
) and continue.Time and Space Complexity:
Code Implementation (Python):
class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
ans, water = 0, capacity # Initialize steps and water level
for i, p in enumerate(plants):
if water >= p: # Sufficient water
water -= p
ans += 1
else: # Insufficient water - refill
water = capacity - p
ans += i * 2 + 1
return ans
Code Implementation (Java):
class Solution {
public int wateringPlants(int[] plants, int capacity) {
int ans = 0, water = capacity;
for (int i = 0; i < plants.length; ++i) {
if (water >= plants[i]) {
water -= plants[i];
ans++;
} else {
water = capacity - plants[i];
ans += i * 2 + 1;
}
}
return ans;
}
}
The other language implementations (C++, Go, TypeScript, Rust, C) follow a similar structure, adapting the syntax appropriately for each language. The core logic remains consistent across all implementations.