Watering Plants - Problem
Imagine you're a dedicated gardener with n plants arranged in a perfect row in your garden. Each plant has its own unique watering needs, and you want to take the best care of them using your trusty watering can.
Here's your setup: Plants are positioned at coordinates x = 0, 1, 2, ..., n-1, and there's a river at position x = -1 where you can refill your watering can to full capacity.
The Rules:
- π± Water plants strictly from left to right (you can't skip ahead)
- π§ If you don't have enough water for the next plant, you must return to the river to refill
- π« No early refills allowed - only refill when absolutely necessary
- π You start at the river (position -1)
- π£ Each move between adjacent positions costs 1 step
Goal: Calculate the minimum total steps needed to water all plants completely.
Input & Output
example_1.py β Basic Garden
$
Input:
plants = [2,2,3,3], capacity = 5
βΊ
Output:
14
π‘ Note:
Start at river(-1)βP0(1 step)βwater P0(capacity=3)βP1(1 step)βwater P1(capacity=1)βcan't water P2βriver(2 steps)βrefillβP2(3 steps)βwater P2βP3(1 step)βwater P3. Total: 1+1+2+3+1+3=14 steps
example_2.py β No Refill Needed
$
Input:
plants = [1,1,1,4,2,3], capacity = 4
βΊ
Output:
30
π‘ Note:
Multiple refill trips are needed due to the plant at index 3 requiring 4 units of water, which uses the full capacity
example_3.py β Single Plant
$
Input:
plants = [7], capacity = 7
βΊ
Output:
1
π‘ Note:
Only need to move from river(-1) to plant 0, which takes 1 step. No refill needed since we have exactly enough water
Constraints
- 1 β€ plants.length β€ 1000
- 1 β€ plants[i] β€ 106
- max(plants[i]) β€ capacity β€ 109
- Important: The capacity is always sufficient to water any individual plant
Visualization
Tap to expand
Understanding the Visualization
1
Initial Setup
Start at the river with full watering can, ready to begin the journey
2
Water Plants
Move to each plant in order, watering them and tracking remaining capacity
3
Smart Refilling
When water runs low for the next plant, return to river to refill
4
Complete Mission
Continue until all plants are properly watered
Key Takeaway
π― Key Insight: This is a pure simulation problem where the optimal strategy is to faithfully follow the watering rules while carefully tracking our position and remaining water capacity.
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code