Hard
Array
DP
Greedy
1326. Minimum Number of Taps to Open to Water a Garden
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:
Example 2:
Constraints:
n
<= 104ranges.length
== n + 1
ranges[i]
<= 100難得這兩天的 Hard 都可以很快想到解法,看解答 intervals 可以只用一維存,晚點再改
JimAug 31, 2023