LeetCode 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:
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]))
相關文章
- LeetCode:動態規劃+貪心題目整理LeetCode動態規劃
- Leetcode 編輯距離(動態規劃)LeetCode動態規劃
- LeetCode 300. 最長上升子序列(Python、動態規劃、貪心演算法)LeetCodePython動態規劃演算法
- 貪心演算法與動態規劃的區別演算法動態規劃
- 演算法---貪心演算法和動態規劃演算法動態規劃
- [leetcode] 動態規劃(Ⅰ)LeetCode動態規劃
- 動態規劃(dynamic programming)與貪心演算法(greedy algorithm)動態規劃演算法Go
- 「演算法思想」分治、動態規劃、回溯、貪心一鍋燉演算法動態規劃
- [leetcode 1235] [動態規劃]LeetCode動態規劃
- leetcode總結——動態規劃LeetCode動態規劃
- leetcode題解(動態規劃)LeetCode動態規劃
- [LeetCode] 2406. Divide Intervals Into Minimum Number of GroupsLeetCodeIDE
- leetcode-動態規劃總結LeetCode動態規劃
- 動態規劃-編輯距離動態規劃
- [LeetCode] 動態規劃題型總結LeetCode動態規劃
- 【leetcode】741 摘櫻桃(動態規劃)LeetCode動態規劃
- LeetCode 動態規劃 House Robber 習題LeetCode動態規劃
- LeetCode 452. Minimum Number of Arrows to Burst Balloons Sort/MediumLeetCode
- LeetCode入門指南 之 動態規劃思想LeetCode動態規劃
- LeetCode 343. 整數拆分--動態規劃LeetCode動態規劃
- LeetCode 分割回文串II(動態規劃)LeetCode動態規劃
- 斜率優化動態規劃優化動態規劃
- 揹包問題演算法全解析:動態規劃和貪心演算法詳解演算法動態規劃
- [LeetCode] 3239. Minimum Number of Flips to Make Binary Grid Palindromic ILeetCode
- Leetcode 貪心:差值調整LeetCode
- Leetcode 題解演算法之動態規劃LeetCode演算法動態規劃
- 【LeetCode】55. 跳躍遊戲 (動態規劃)LeetCode遊戲動態規劃
- 122. 買賣股票的最佳時機 II-簡單-動態規劃、貪心演算法動態規劃演算法
- 圖解Leetcode組合總和系列——回溯(剪枝優化)+動態規劃圖解LeetCode優化動態規劃
- 動態規劃動態規劃
- python 實現 割繩子問題(劍指offer 14題) 動態規劃 或者貪心演算法Python動態規劃演算法
- [LeetCode 中等 動態規劃 ]221. 最大正方形LeetCode動態規劃
- [leetcode初級演算法]動態規劃總結LeetCode演算法動態規劃
- 離散化
- 2024.9.6 leetcode 70 爬樓梯 (雜湊表/動態規劃)LeetCode動態規劃
- Leetcode 題解系列 -- 股票的最大利潤(動態規劃)LeetCode動態規劃
- 牛客練習賽26 C 城市規劃【思維+貪心】
- 動態規劃法動態規劃