2Brute2Best
πŸ”₯0

Minimum Number of Taps to Open to Water a Garden

HardΒ·LeetCode #1326 Pro OnlyTricky / Edge Case
LeetCode
01

Problem Statement

A garden lies along the x-axis from point 0 to point n, so its length is n. There are n + 1 taps, one at every whole point 0, 1, …, n.

Tap i, when opened, waters everything from i βˆ’ ranges[i] to i + ranges[i].

Return the fewest taps you need to open so that the whole garden [0, n] is watered. If that's impossible, return -1.

Input

An integer n and an array ranges of length n + 1.

Output

One integer: the minimum number of taps, or -1.

Example 1
Input: n = 5, ranges = [3, 4, 1, 1, 0, 0]
Output: 1

Tap 1 waters [βˆ’3, 5], which covers the whole garden [0, 5] on its own.

Example 2
Input: n = 3, ranges = [0, 0, 0, 0]
Output: -1

Every tap waters only its own point. The ground between the points stays dry.

Constraints
  • β€’ 1 ≀ n ≀ 10^4
  • β€’ ranges.length == n + 1
  • β€’ 0 ≀ ranges[i] ≀ 100
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œtap i waters [i βˆ’ ranges[i], i + ranges[i]]”
β†’ Each tap is an interval. Turn the input into intervals first.
β€œwater the whole garden”
β†’ Cover a whole range with no gaps β†’ Video Stitching.
β€œminimum number of taps, or βˆ’1”
β†’ Fewest jumps, and detect a gap where the reach stops growing.
03

Approach Complexity Ladder

Brute Force β€” try every set of tapsO(2^N)O(N)

Turn taps into intervals, then try opening or skipping each one

Better β€” DP over garden pointsO(N log N + N Β· R)O(N)

dp[x] = fewest taps watering [0, x]; each tap updates the points it covers

Optimal β€” maxReach array + Jump Game IIO(N)O(N)

maxReach[l] = farthest end of a tap starting at l; then jump greedily to the farthest reach

Brute Force Intuition

Turn each tap into the interval [max(0, i βˆ’ r), min(n, i + r)] and sort by left end. Try every open / closed choice. A tap can be opened only if it starts inside the part already watered, since later taps start even further right. Record the fewest taps that reach n.

Recognition rule for this sub-pattern:

Pick or remove intervals to optimise coverage. Sorting by end and keeping the earliest finisher leaves the most room.
Remember this Β· Intervals
Trigger
Input is [start, end] pairs; overlap, merge, insert, meeting rooms, free time, minimum removals.
Think
After sorting, do I only ever need to compare against the most recent interval?
Key question
Sort by start or by end β€” which one makes the greedy choice obvious?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Intervals translate into production architectures and low-level memory mechanics.