There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e., the length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden.
Constraints:
1 <= n <= 104ranges.length == n + 10 <= ranges[i] <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a long garden and a collection of water taps placed at different spots, each capable of watering a certain distance around it. The brute force strategy involves testing every possible combination of opening these taps to see which combination covers the entire garden using the fewest number of taps.
Here's how the algorithm would work step-by-step:
import itertools
def min_taps(garden_length, tap_ranges):
available_taps = []
for tap_index in range(len(tap_ranges)):
left_bound = max(0, tap_index - tap_ranges[tap_index])
right_bound = min(garden_length, tap_index + tap_ranges[tap_index])
available_taps.append((left_bound, right_bound))
# Exhaustively test every combination size starting from smallest to guarantee minimum taps
for combination_size in range(1, len(available_taps) + 1):
for tap_combination in itertools.combinations(available_taps, combination_size):
current_coverage = 0
sorted_taps = sorted(tap_combination, key=lambda single_tap: single_tap[0])
# Validate if the current set of taps forms a contiguous chain starting from position zero
if sorted_taps[0][0] <= 0:
for tap_interval in sorted_taps:
if tap_interval[0] <= current_coverage:
current_coverage = max(current_coverage, tap_interval[1])
else:
break
# Return the size immediately since combinations are checked in ascending order
if current_coverage >= garden_length:
return combination_size
return -1Instead of checking every single combination of water spouts, we can use a strategy similar to making stepping stones across a river. By looking at how far each spout can reach from any given spot, we can figure out the absolute farthest we can stretch our coverage with the fewest turns.
Here's how the algorithm would work step-by-step:
class Solution:
def minTaps(self, garden_length: int, tap_ranges: list[int]) -> int:
maximum_reach = [0] * (garden_length + 1)
for tap_index, tap_radius in enumerate(tap_ranges):
left_boundary = max(0, tap_index - tap_radius)
right_boundary = min(garden_length, tap_index + tap_radius)
maximum_reach[left_boundary] = max(maximum_reach[left_boundary], right_boundary)
# We track the current end of our covered path and the farthest we can reach next.
current_end = 0
farthest_reach = 0
tap_count = 0
current_index = 0
while current_end < garden_length:
while current_index <= current_end and current_index <= garden_length:
farthest_reach = max(farthest_reach, maximum_reach[current_index])
current_index += 1
# If we cannot extend our reach further, the garden cannot be fully watered.
if current_end == farthest_reach:
return -1
current_end = farthest_reach
tap_count += 1
# Once our covered path spans the entire garden, return the total taps turned on.
return tap_count| Case | How to Handle |
|---|---|
| Garden length n is 0 | Return 0 immediately since no taps are needed to cover a zero-length garden. |
| Impossible to cover the entire garden from 0 to n | Detect unreachable intervals during the greedy expansion and return -1. |
| Taps have a range of 0 | Ignore zero-range taps as they cannot expand coverage beyond their single point. |
| Taps with excessively large ranges that cover beyond the garden boundaries | Clamp the effective coverage intervals to the valid garden range of 0 to n. |
| A single tap is sufficient to cover the entire garden | The greedy jump game logic correctly identifies this and returns 1. |
| Multiple overlapping taps with varying ranges | Track the maximum reachable right bound for each starting left bound to greedily pick the best tap. |