Skip to content
Decoder

Minimum Number of Taps to Open to Water a Garden

Hard

Problem

A garden lies on a line from 0 to n. There are n + 1 taps at positions 0, 1, ..., n. Tap i, when open, waters [i - ranges[i], i + ranges[i]]. Return the minimum number of taps to open so that the whole garden [0, n] is watered, or -1 if that is impossible.

Example 1

Input
n = 5, ranges = [3, 4, 1, 1, 0, 0]
Output
1
Why
tap 1 waters [-3, 5]

Example 2

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

Example 3

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

Constraints

  • 1 <= n <= 10^4
  • ranges.length == n + 1
  • 0 <= ranges[i] <= 100

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.