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
🌿 The Efficient Garden Watering Process 🌿RIVERπŸ’§ RefillStationPosition: -1🌱P0needs: 2🌿P1needs: 2🌲P2needs: 3🌺P3needs: 31 step1 stepReturn to refill!πŸ“Š Step-by-Step BreakdownInitial: At river (-1), capacity = 5Step 1: Riverβ†’P0 (1 step), water P0, capacity = 3Step 2: P0β†’P1 (1 step), water P1, capacity = 1Check: P2 needs 3, but we only have 1Step 3: P1β†’River (2 steps), refill to capacity = 5Step 4: Riverβ†’P2 (3 steps), water P2, capacity = 2Step 5: P2β†’P3 (1 step), water P3, capacity = -1Total Steps: 1 + 1 + 2 + 3 + 1 = 8πŸ’‘ Key: Only refill when absolutely necessary!
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.
Asked in
Amazon 45 Google 35 Microsoft 25 Meta 20
38.4K Views
Medium Frequency
~15 min Avg. Time
1.6K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen