LeetCode 1326. Minimum Number of Taps to Open to Water a Garden 动态规划 离散化 贪心

231 篇文章 0 订阅
120 篇文章 1 订阅

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]))

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值