Minimum Number of Taps to Open to Water a Garden
Hard
Topics
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
Input: ranges = [3,4,1,1,0,0], n = 5 Output: 1
Example 2
Input: ranges = [0,0,0,0], n = 3 Output: -1
Example 3
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
- For each tap, compute its clipped interval and keep the best right end per left endpoint.
- Sweep with
cur(covered so far) andnxt(best reachable). - Advance a pointer over all left endpoints up to
cur, folding them intonxt. - If
nxt <= cur, return -1. Otherwise count a tap and setcur = 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 advancesComplexity
- 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 < curinstead ofnxt <= cur. Equality means no progress, which is just as fatal as going backwards. - Reading the garden as
[0, n-1]. There arek+1taps covering the interval[0, k], so the target isk, 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 tapsimport java.util.*;
class Solution {
public int minTaps(int[] nums, int k) {
int[] ranges = nums;
int n = k;
int[] maxReach = new int[n + 1];
for (int i = 0; i < ranges.length; i++) {
int left = Math.max(0, i - ranges[i]), right = Math.min(n, i + ranges[i]);
if (right > maxReach[left]) maxReach[left] = right;
}
int taps = 0, cur = 0, nxt = 0, p = 0;
while (cur < n) {
while (p <= cur) {
if (maxReach[p] > nxt) nxt = maxReach[p];
p++;
}
if (nxt <= cur) return -1;
taps++;
cur = nxt;
}
return taps;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int minTaps(vector<int>& nums, int k) {
vector<int>& ranges = nums;
int n = k;
vector<int> maxReach(n + 1, 0);
for (int i = 0; i < (int)ranges.size(); i++) {
int left = max(0, i - ranges[i]), right = min(n, i + ranges[i]);
if (right > maxReach[left]) maxReach[left] = right;
}
int taps = 0, cur = 0, nxt = 0, p = 0;
while (cur < n) {
while (p <= cur) {
if (maxReach[p] > nxt) nxt = maxReach[p];
p++;
}
if (nxt <= cur) return -1;
taps++;
cur = nxt;
}
return taps;
}
};/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/ var minTaps = function(nums, k) {
const ranges = nums, n = k;
const maxReach = new Array(n + 1).fill(0);
for (let i = 0; i < ranges.length; i++) {
const left = Math.max(0, i - ranges[i]), right = Math.min(n, i + ranges[i]);
if (right > maxReach[left]) maxReach[left] = right;
}
let taps = 0, cur = 0, nxt = 0, p = 0;
while (cur < n) {
while (p <= cur) {
if (maxReach[p] > nxt) nxt = maxReach[p];
p++;
}
if (nxt <= cur) return -1;
taps++;
cur = nxt;
}
return taps;
};