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 [iranges[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:

  • 11 \leq n 104\leq 10^4

  • ranges.length ==== n + 1

  • 00 \leq ranges[i] 100\leq 100

Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths
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 [iranges[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:

  • 11 \leq n 104\leq 10^4

  • ranges.length ==== n + 1

  • 00 \leq ranges[i] 100\leq 100

Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths