Minimum Number of Taps to Open to Water a Garden - Problem
Imagine you're a smart irrigation system designer tasked with watering a rectangular garden efficiently! ๐ŸŒฑ

You have a one-dimensional garden that stretches from point 0 to point n on the x-axis. Along this garden, there are n + 1 water taps positioned at every integer point: [0, 1, 2, ..., n].

Each tap has a specific watering range. The tap at position i can water the area from [i - ranges[i], i + ranges[i]] when turned on. For example, if ranges[2] = 3, then tap 2 can water the area from position -1 to position 5.

Your goal: Find the minimum number of taps you need to turn on to water the entire garden from position 0 to position n. If it's impossible to water the whole garden, return -1.

This is essentially an interval covering problem - you need to cover the interval [0, n] using the minimum number of available intervals (tap ranges).

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 5, ranges = [3,4,1,1,0,0]
โ€บ Output: 1
๐Ÿ’ก Note: Tap at index 1 has range 4, covering area [1-4, 1+4] = [-3, 5]. Since we only need to cover [0, 5], this single tap is sufficient.
example_2.py โ€” Multiple Taps Needed
$ Input: n = 3, ranges = [0,0,0,0]
โ€บ Output: -1
๐Ÿ’ก Note: All taps have range 0, meaning each can only water its own position. Cannot cover the gaps between positions, making it impossible to water the entire garden.
example_3.py โ€” Optimal Selection
$ Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
โ€บ Output: 3
๐Ÿ’ก Note: Optimal solution uses taps at indices 1, 4, and 6. Tap 1 covers [โˆ’1,3], tap 4 covers [2,6], and tap 6 covers [6,6], together covering [0,7].

Visualization

Tap to expand
๐ŸŒฑ Garden [0, 7] ๐ŸŒฑ01234567๐Ÿ’ง T0Range: 1๐Ÿ’ง T1Range: 2๐Ÿ’ง T4Range: 2๐Ÿ’ง T7Range: 1โœ“ Selected: T1โœ“ Selected: T4โœ“ Selected: T7๐ŸŽฏ Optimal Solution: 3 tapsGreedy strategy: Always choose tap with maximum coverage extension
Understanding the Visualization
1
Map Tap Ranges
Convert each tap's position and range into coverage intervals
2
Start from Beginning
Begin at position 0 and look for taps that can cover this position
3
Choose Greedily
Among valid taps, pick the one that extends coverage the furthest right
4
Advance Coverage
Move to the new coverage boundary and repeat until garden is fully covered
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works because extending coverage as far as possible at each step minimizes the total number of taps needed. This transforms a complex optimization problem into a simple iterative process.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(2^n ร— n)

2^n subsets to check, each taking O(n) time to validate coverage

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Space to store current subset and coverage tracking

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค n โ‰ค 104
  • ranges.length == n + 1
  • 0 โ‰ค ranges[i] โ‰ค 100
  • Garden coordinates are from 0 to n inclusive
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
34.2K Views
Medium Frequency
~25 min Avg. Time
1.5K 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