Minimum Number of Taps to Open to Water a Garden

Hard

Topics
ArrayDynamic ProgrammingGreedy

A garden is a one-dimensional interval [0, k]. There are nums.length == k + 1 taps, one at each integer point i. Tap i waters the area [i - nums[i], i + nums[i]]. Return the minimum number of taps to open so the whole garden is watered, or -1 if it is impossible.

Example 1

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

Example 2

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

Example 3

1021120324150617
Input:  ranges = [1,2,1,0,2,1,0,1], n = 7
Output: 3

Constraints

  • 1 <= k <= 10^4
  • nums.length == k + 1
  • 0 <= nums[i] <= 100
Editorial — intuition, approach, complexity and pitfalls

Intuition

Each tap covers an interval, so this is interval cover on a line — and interval cover with minimum count is the same shape as jump game II. Convert every tap to its interval, then ask: from everything reachable so far, how far right can one more tap take me?

The conversion step matters. Several taps can share a left endpoint, and only the one reaching furthest right is ever worth opening, so collapse them: maxReach[left] is the best right end among taps starting at left. That turns a set of intervals into a jump table.

Then sweep. Hold the currently covered prefix [0, cur] and the furthest point nxt reachable by opening one more tap whose interval starts inside that prefix. When cur is exhausted, commit a tap and jump to nxt. If nxt never advances past cur, there is a gap and the garden cannot be covered.

Approach

  1. For each tap, compute its clipped interval and keep the best right end per left endpoint.
  2. Sweep with cur (covered so far) and nxt (best reachable).
  3. Advance a pointer over all left endpoints up to cur, folding them into nxt.
  4. If nxt <= cur, return -1. Otherwise count a tap and set cur = nxt.

Dry Run

nums = [3,4,1,1,0,0]   k = 5

  tap  radius  covers
  0    3       [0,3]
  1    4       [0,5]
  2    1       [1,3]
  3    1       [2,4]
  4    0       [4,4]
  5    0       [5,5]

best right reach per left endpoint: [5, 3, 4, 0, 4, 5]
  left 0 keeps 5, because tap 1 beats tap 0

sweep
  cur = 0, fold in left endpoint 0 -> nxt = 5
  nxt 5 > cur 0, so open one tap and jump to 5
  cur = 5 = k, done

answer 1

  [0,0,0,0] with k = 3 -> -1: every interval is a single point, nothing advances

Complexity

  • Time — O(n): the interval table is one pass, and the sweep's inner pointer never resets, so the two loops together are linear.
  • Space — O(n) for the reach table.

Common Pitfalls

  • Sorting the intervals and greedily taking the longest. Sorting is O(n log n) for information the bucket-by-left-endpoint table already encodes in O(n), and "longest" is not the right criterion — reach from the current prefix is.
  • Resetting the inner pointer on each outer iteration, which makes the sweep O(n²).
  • Forgetting to clip intervals to [0, k]. A tap at position 0 with radius 100 covers [-100, 100], and unclipped indices either crash or silently alias.
  • Treating a tap with radius 0 as covering nothing. It covers the single point [i, i], which never advances coverage but is not an error.
  • Checking nxt < cur instead of nxt <= cur. Equality means no progress, which is just as fatal as going backwards.
  • Reading the garden as [0, n-1]. There are k+1 taps covering the interval [0, k], so the target is k, not the array length.

Full Solution — all languages

class Solution:
    def minTaps(self, nums, k):
        ranges = nums
        n = k
        max_reach = [0] * (n + 1)
        for i in range(len(ranges)):
            left = max(0, i - ranges[i])
            right = min(n, i + ranges[i])
            if right > max_reach[left]:
                max_reach[left] = right
        taps = 0
        cur = 0
        nxt = 0
        p = 0
        while cur < n:
            while p <= cur:
                if max_reach[p] > nxt:
                    nxt = max_reach[p]
                p += 1
            if nxt <= cur:
                return -1
            taps += 1
            cur = nxt
        return taps
SensAI
Beta
Tap the mic to stop voice mode