Tutorialspoint
Problem
Solution
Submissions

Minimum Number of Taps to Water Garden

Certification: Advanced Level Accuracy: 100% Submissions: 1 Points: 15

Write a Java program to determine the minimum number of taps needed to water an entire garden. You have a garden of length n where each position has a tap with a specific range. The range of a tap at position i is given by an array ranges, where ranges[i] represents the covering range of the tap at position i. A tap at position i can water the area from [i - ranges[i], i + ranges[i]]. The garden needs to be watered from position 0 to position n. Return the minimum number of taps that need to be turned on to water the entire garden. If it is impossible to water the entire garden, return -1.

Example 1
  • Input: n = 5, ranges = [3,4,1,1,0,0]
  • Output: 1
  • Explanation:
    The tap at position 0 has a range of 3, covering positions [0-3, 0+3] = [0, 3].
    The tap at position 1 has a range of 4, covering positions [1-4, 1+4] = [0, 5].
    The tap at position 1 can water the entire garden [0, 5], so we only need 1 tap.
Example 2
  • Input: n = 3, ranges = [0,0,0,0]
  • Output: -1
  • Explanation:
    The tap at position 0 has a range of 0, covering only position 0.
    The tap at position 1 has a range of 0, covering only position 1.
    The tap at position 2 has a range of 0, covering only position 2.
    The tap at position 3 has a range of 0, covering only position 3.
    It's impossible to cover the entire garden [0, 3] with these taps.
Constraints
  • 1 <= n <= 10^4
  • ranges.length == n + 1
  • 0 <= ranges[i] <= 100
  • Time Complexity: O(n)
  • Space Complexity: O(n)
ArraysNumberPwCArctwist
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Convert the problem to the "Jump Game" format by calculating the intervals each tap can cover
  • For each tap at position i, calculate the leftmost position start = max(0, i - ranges[i]) and the rightmost position end = min(n, i + ranges[i])
  • Sort the intervals by their start positions
  • Use a greedy approach to find the minimum number of taps needed
  • Iterate through the sorted intervals and find the interval that extends furthest to the right from the current position

Steps to solve by this approach:

 Step 1: Convert the tap ranges to maximum reachable positions from each starting point.

 Step 2: For each position i, calculate the leftmost (start = max(0, i - ranges[i])) and rightmost (end = min(n, i + ranges[i])) positions.
 Step 3: Store the maximum reach for each starting position in an array maxReach.
 Step 4: Use a greedy approach similar to the Jump Game problem to find the minimum taps needed.
 Step 5: Maintain two pointers: currEnd (the current end position covered) and nextEnd (the furthest position that can be  reached).
 Step 6: Increment the tap count whenever we need to use a new tap to advance further.
 Step 7: If at any point we cannot advance further, return -1.

Submitted Code :