We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Minimum Number of Taps to Open to Water a Garden

Number: 1451

Difficulty: Hard

Paid? No

Companies: Salesforce, DE Shaw, Amazon, Meesho, Intuit, Bloomberg, Google, Microsoft, ServiceNow, BNY Mellon, Akuna Capital


Problem Description

Given a one-dimensional garden represented by the interval [0, n] and n+1 water taps placed at positions 0 through n, each tap i can water an interval defined by [i - ranges[i], i + ranges[i]]. The goal is to determine the minimum number of taps needed such that their watered intervals cover the whole garden. If it is impossible to water the entire garden, return -1.


Key Insights

  • The problem can be transformed into an interval covering problem.
  • Each tap defines an interval. Preprocess taps to calculate the farthest right point each interval can cover.
  • A greedy approach can be used similar to the "jump game" strategy, trying to extend coverage as far as possible.
  • Checking if the current interval cannot be extended further is crucial to determine if the garden cannot be fully watered.

Space and Time Complexity

Time Complexity: O(n) – Preprocessing the intervals takes O(n) and the greedy traversal is O(n). Space Complexity: O(n) – An additional array is used to store the maximum reach for each starting point.


Solution

The solution begins by converting each tap’s position and range into an interval. Instead of considering individual intervals directly, we create an array (maxRange) where for any index i, maxRange[i] stores the farthest point we can reach from any tap that starts its interval at i. Using this preprocessed information, a greedy algorithm scans the garden from left to right. At every point i within the current reachable region, we update the next farthest reach. Once we reach the end of the current region, we select that as our new coverage endpoint and increment our tap count. If at any point the next coverage region does not extend beyond the current, the garden cannot be fully watered and the answer is -1.


Code Solutions

# Python solution to the problem

def minTaps(n, ranges):
    # Initialize an array to store the farthest we can reach from each starting point
    max_reach = [0] * (n + 1)
    
    # Pre-process each tap to update the maximum reach
    for i, r in enumerate(ranges):
        left = max(0, i - r)  # start of the interval, not less than 0
        right = min(n, i + r)  # end of the interval, not more than n
        max_reach[left] = max(max_reach[left], right)
    
    taps_opened = 0  # Count of taps used
    current_end = 0  # Current reached end of the garden
    next_end = 0  # Next reachable end with one more tap
    
    # Traverse through the garden
    for i in range(n + 1):
        # Update the next_end value at current index i
        next_end = max(next_end, max_reach[i])
        
        # If we have reached the end of the current coverage,
        # it's time to use another tap
        if i == current_end:
            taps_opened += 1
            current_end = next_end
            # If current_end has reached or exceeded the garden length, return answer
            if current_end >= n:
                return taps_opened
    return -1  # If the garden is not fully covered, return -1

# Example usage:
print(minTaps(5, [3,4,1,1,0,0]))  # Output: 1
print(minTaps(3, [0,0,0,0]))      # Output: -1
← Back to All Questions