You are given an array plants
where plants[i]
represents the amount of water the i-th plant needs. You also have a watering can with a fixed capacity capacity
. You start at the beginning of the row of plants (index 0) with a full watering can, and move from left to right, watering each plant in order.
Constraints: Each plant must be watered in order. You can only refill at the river. The watering can's capacity is fixed and does not change.
At first glance, the problem seems to require simulating each step: walk to each plant, check if you have enough water, and refill if needed. A brute-force approach would be to simulate every single step, including all movements back and forth to the river, which could be inefficient.
However, we can optimize by recognizing that we only need to track:
The key insight is to count steps efficiently by summing the walk to each plant, and only adding the extra steps for refills when necessary.
We will solve the problem using a simple simulation with optimized step counting:
steps
to 0 (to count total steps), and water
to capacity
(current water in the can).i
, check if water
is at least plants[i]
.water -= plants[i]
), and increment steps
by 1 (move to next plant).2 * i
(go back i
steps, return i
steps), refill the can, then water the plant.water
by plants[i]
and increment steps
by 1 (for watering this plant).steps
.This approach ensures that we only add extra steps when a refill is needed, and otherwise simply move from plant to plant.
Let's consider plants = [2, 4, 5, 1, 2]
and capacity = 6
.
Final answer: 17 steps.
The Watering Plants problem can be solved efficiently by simulating the process and only counting extra steps when a refill is necessary. By iterating over the plants and tracking the remaining water, we avoid unnecessary computation and keep the solution clean and efficient. The algorithm is straightforward, easy to implement, and runs in linear time with constant space.
class Solution:
def wateringPlants(self, plants, capacity):
steps = 0
water = capacity
for i, need in enumerate(plants):
if water >= need:
steps += 1
water -= need
else:
# Go back to river and return
steps += 2 * i + 1
water = capacity - need
return steps
class Solution {
public:
int wateringPlants(vector<int>& plants, int capacity) {
int steps = 0;
int water = capacity;
for (int i = 0; i < plants.size(); ++i) {
if (water >= plants[i]) {
steps += 1;
water -= plants[i];
} else {
steps += 2 * i + 1;
water = capacity - plants[i];
}
}
return steps;
}
};
class Solution {
public int wateringPlants(int[] plants, int capacity) {
int steps = 0;
int water = capacity;
for (int i = 0; i < plants.length; i++) {
if (water >= plants[i]) {
steps += 1;
water -= plants[i];
} else {
steps += 2 * i + 1;
water = capacity - plants[i];
}
}
return steps;
}
}
var wateringPlants = function(plants, capacity) {
let steps = 0;
let water = capacity;
for (let i = 0; i < plants.length; i++) {
if (water >= plants[i]) {
steps += 1;
water -= plants[i];
} else {
steps += 2 * i + 1;
water = capacity - plants[i];
}
}
return steps;
};