Tap here to switch tabs
Problem
Submissions

Problem: Minimum Number of Taps to Open to Water a Garden

med
30 min
Try to solve the Minimum Number of Taps to Open to Water a Garden problem.

Statement

A one-dimensional garden lies on the x-axis, starting at point 00 and ending at point n. There are n + 1 taps positioned at integer points [0,1,…,n][0, 1, \ldots, n] along the garden.

You are given an integer n and an integer array ranges of length n + 1, where ranges[i] represents the reach of the ii-th tap. When opened, the ii-th tap waters the interval [i−ranges[i],i+ranges[i]][i - \text{ranges}[i], i + \text{ranges}[i]].

Return the minimum number of taps that must be opened to water the entire garden [0,n][0, n]. If it is impossible to water the entire garden, return −1-1.

Constraints:

  • 1≤1 \leq n ≤104\leq 10^4

  • ranges.length ==== n + 1

  • 0≤0 \leq ranges[i] ≤100\leq 100

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Number of Taps to Open to Water a Garden

med
30 min
Try to solve the Minimum Number of Taps to Open to Water a Garden problem.

Statement

A one-dimensional garden lies on the x-axis, starting at point 00 and ending at point n. There are n + 1 taps positioned at integer points [0,1,…,n][0, 1, \ldots, n] along the garden.

You are given an integer n and an integer array ranges of length n + 1, where ranges[i] represents the reach of the ii-th tap. When opened, the ii-th tap waters the interval [i−ranges[i],i+ranges[i]][i - \text{ranges}[i], i + \text{ranges}[i]].

Return the minimum number of taps that must be opened to water the entire garden [0,n][0, n]. If it is impossible to water the entire garden, return −1-1.

Constraints:

  • 1≤1 \leq n ≤104\leq 10^4

  • ranges.length ==== n + 1

  • 0≤0 \leq ranges[i] ≤100\leq 100