What the Watering Plants problem actually is
Watering Plants (LeetCode 2079) hands you a row of plants, each with a fixed water requirement, a can that holds capacity units, and a river at position −1; you walk left to right, one step per plant, and whenever the water left in the can is less than the next plant’s need, you walk back to the river, refill, and walk out again. The intended solution is a single left-to-right simulation in O(n) time and O(1) extra space — no sorting, no dynamic programming — because the walking order is fixed and refilling only at the moment you run short is the cheapest policy. What makes it more than a toy is that the cost of a refill depends entirely on where it happens, which is also the thing that decides whether a real watering round takes twenty minutes or an hour.

Three things that actually matter
1. Fill ratio, not can size
Divide capacity by average plant demand and you get plants per fill. A 10 L can against pots that take 0.5 L each covers 20 plants; the same can against 2 L hungry tomato plants covers five. The LeetCode array encodes exactly this ratio, which is why a capacity of 6 against plants of 2, 4, 5, 1, 2 produces two refills while a capacity of 8 produces one. Litres are the wrong unit to argue about — the ratio is.
2. Where the refills land
Standing at plant i−1 with too little water, the round trip back to the source and out to plant i costs i steps back plus i+1 steps forward, or 2i+1. Refilling before plant 2 costs 5 steps; before plant 40 it costs 81. Two refills at the far end of a row can outweigh ten near the start, so the total is dominated by the last refill, not the first. In a polytunnel this is the mechanic behind the far bed getting skipped on hot afternoons.
3. The order constraint
You cannot reorder the array, and that restriction is doing a lot of work. Real beds allow reordering: put the thirstiest containers nearest the tap, group them so one fill clears a block, or replace the walk entirely with a hose. Once reordering or a second water source is allowed, the problem stops being a simulation and becomes a batching question — a different puzzle with a different answer.
Numbers worth knowing
The arithmetic is easy to check by hand. Below is a five-plant row needing 2, 4, 5, 1 and 2 units, worked at several capacities using the standard greedy rule; the non-monotonic jump between capacity 8 and capacity 12 is not a typo.
| Capacity | Refills | Refill positions | Total steps |
|---|---|---|---|
| 5 | 3 | plants 1, 2, 3 | 17 |
| 6 | 2 | plants 2, 4 | 17 |
| 8 | 1 | plant 2 | 9 |
| 12 | 1 | plant 4 | 13 |
| 14 or more | 0 | — | 5 |
Mapping the parameters onto field units is where the model earns its keep. The numbers below are typical ranges for reference, not standards:
| Problem parameter | Field equivalent | Typical value |
|---|---|---|
| capacity | watering can or sprayer tank | 5 L, 10 L, or a 16 L backpack unit |
| plants[i] | water per container per pass, summer | 0.3–1.5 L for most patio crops |
| one step | spacing between pots | 0.6–0.8 m |
| the river | tap, water butt, or trough | often 10–30 m from the far bed |
| 2i+1 refill cost | walk to the tap and back | 20–60 seconds at a brisk pace |
Walking pace unladen is roughly 1.2–1.4 m/s; with a full 10 L can it drops noticeably, and that slowdown compounds on every refill. Water-holding amendments shift the equation too — a super-absorbent polymer dose of typically 1–3 g per litre of substrate is often quoted for containers, and the practical effect is fewer passes rather than less water per pass. If you are comparing grades, the super absorbent polymer guide covers absorbency under load and gel strength, which is what actually determines whether the saving survives a hot week.
When the usual advice is wrong
“A bigger can always saves trips.” The table above shows capacity 8 finishing in 9 steps while capacity 12 takes 13. Carrying more defers the refill to a later, more expensive position, and 2i+1 punishes lateness. The optimum sits where the fill exactly clears the row or clears a block that ends near the tap — not at maximum capacity.
“Refill only when the can is empty.” That is provably right inside the problem, where the river is reachable only by a dedicated trip. In a real garden the path loops past the tap, so topping off in passing costs nothing and removes a future round trip. The model’s biggest simplification is not the fixed water demand; it is the assumption that the source is out of the way.
“Give every plant the same amount.” The array forces uniform treatment per plant, but a tray of seedlings and a fruiting aubergine in the same row do not share a demand. Fixed doses either drown the small pots or short the heavy ones, and runoff from the overwatered ones is pure loss.
“Water more often and you cannot go wrong.” Frequency is the variable the simulation never touches. Deeper, less frequent watering pushes roots down and usually beats daily light passes, and mulch or polymer reduces evaporation between passes. For scheduling by soil type and moisture rather than by habit, the irrigation and soil management guide goes deeper than a can-and-row model can.
A quick way to test it yourself
You can reproduce the whole thing in an afternoon with a marked can and twenty containers. The point is to find your own refill positions, because they rarely sit where you assume.
- Mark a 10 L can at 2 L intervals with tape, and a second can at 5 L.
- Line up 20 containers and estimate each one’s demand by weight before and after a normal watering, or by timed flow from a known-rate can.
- Water the row with the 10 L can, counting refills and the plant index at each refill. Note the position, not just the count.
- Compute 2i+1 for each refill. The largest number is your bottleneck and usually explains why the round felt slow.
- Repeat with the 5 L can on a different day at similar temperature. Compare total walking time, not total water.
- Amend half the containers with polymer at the label rate, wait a week, and repeat step 3. Compare refill counts rather than litres applied.
- If the refill walk dominates, price a hose or drip line against the labour; that comparison decides the question faster than any can upgrade.
Container growers will recognise most of this already — the home gardening watering and container guide covers pot size, drainage, and how quickly small volumes swing, which is where the fixed-demand assumption breaks first.
FAQ
Is refilling only when the can runs dry always optimal in Watering Plants?
Within the problem’s rules, yes. The standard argument is an exchange one: because capacity never changes and plants must be visited in order, an earlier refill adds a full round trip without removing any later one, so it can only add steps. Relax the rules — allow a second source, reordering, or a refill point you pass anyway — and the greedy answer is no longer automatically the right one.
What are the time and space complexity?
O(n) time for one pass over the array and O(1) extra space, since you only track the remaining water and the running step count. The simulation is the intended solution; sorting or searching is unnecessary because the plant order is fixed input.
Does the same logic apply to drip irrigation?
Only partly. Drip removes the refill walk almost entirely, so the binding constraints become flow rate, emitter spacing, and run time rather than can capacity. The 2i+1 penalty disappears, which is exactly why drip wins on long rows and loses on five scattered pots.
Practical note: if you are sourcing cans, polymer, or drip components and want the numbers checked against a specific row length and crop, send the specifications — row length, container volume, water source distance, and target refill frequency — and we will work the arithmetic with you.