LeetCode 1326. Minimum Number of Taps to Open to Water a Garden 動態規劃 離散化 貪心

taoqick發表於2020-10-22

There is a one-dimensional garden on the x-axis. The garden starts at the point 0and 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.

Example 3:

Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3

Example 4:

Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2

Example 5:

Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1

 

Constraints:

  • 1 <= n <= 10^4
  • ranges.length == n + 1
  • 0 <= ranges[i] <= 100

-----------------------------

思路一:動態規劃

思路一比較容易想到,用f[pos]表示覆蓋[0,pos]閉區間至少需要多少個水龍頭,那麼f[pos]=min(f[pos],f[start]+1),pos是i位置水龍頭覆蓋的範圍[start,end],那麼程式碼如下:

class Solution:
    def minTaps(self, n: int, ranges: List[int]) -> int:
        f = [0] + [n+1]*n
        for i in range(n+1):
            start,end = max(i-ranges[i],0),min(i+ranges[i],n)
            for pos in range(start, end+1):
                f[pos] = min(f[pos],f[start]+1)
        #print(f)
        return -1 if f[n] == n+1 else f[n]

思路一每個位置都需要覆蓋,複雜度n*range,range是水龍頭平均覆蓋範圍。

思路二:思路二分多個階段,在[cur_stage_start,cur_stage_end]中的水龍頭,最遠覆蓋到furthest。如果furthest比cur_stage_end遠,那麼下個階段是[cur_stage_end,furthest],此時跳到furthest的水龍頭一定需要,cnt+1;如果furthest比cur_stage_end近,不可能全覆蓋,返回-1。所以初始的[cur_stage_start,cur_stage_end]=[0,0]這個水龍頭即可。

另外一個技巧是,可以存一個離散化的陣列right_edge= [0]*(n+1)   #key:left edge; value: right edge。可以預先知道每個位置跳到的最遠位置在哪裡

from typing import List
class Solution:
    def minTaps(self, n: int, ranges: List[int]) -> int:
        right_edge= [0]*(n+1)   #key:left edge; value: right edge

        for i in range(n+1):
            l,r = max(0,i-ranges[i]),min(n,i+ranges[i])
            right_edge[l] = r
        cur_stage_end,furthest,cnt = 0,0,0
        for i in range(n): #bug1: n instead of n+1
            if (right_edge[i] > furthest):
                furthest = right_edge[i]
            if (i == cur_stage_end):
                if (furthest <= cur_stage_end):
                    return -1
                else:
                    cur_stage_end,furthest,cnt = furthest,i,cnt+1
        return cnt

s = Solution()
print(s.minTaps(n = 7, ranges = [1,2,1,0,2,1,0,1]))

 

 

相關文章