Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

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

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
Run ⌘' · Submit ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access