leetcode - 1326. Minimum Number of Taps to Open to Water a Garden

这篇具有很好参考价值的文章主要介绍了leetcode - 1326. Minimum Number of Taps to Open to Water a Garden。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Description

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:
leetcode - 1326. Minimum Number of Taps to Open to Water a Garden,OJ题目记录,leetcode,算法,职场和发展

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 <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100

Solution

For example2, we need to water the interval between 1 and 2.

Draw the range that each water tap could cover, like example1 does, then this problem is converted into 45. 跳跃游戏 II.

Build jump list, jump[i - span] = i + span

Then use greedy to solve jump game.

Time complexity: o ( n ) o(n) o(n)
Space complexity: o ( 1 ) o(1) o(1)文章来源地址https://www.toymoban.com/news/detail-687052.html

Code

class Solution:
    def minTaps(self, n: int, ranges: List[int]) -> int:
        intervals = [i for i in range(n + 1)]
        for index, each_range in enumerate(ranges):
            left = max(0, index - each_range)
            right = min(n, index + each_range)
            intervals[left] = max(intervals[left], right)
        res = 0
        cur_max = -1
        next_max = intervals[0]
        for i, jump in enumerate(intervals):
            if i > next_max:
                return -1
            if i > cur_max:
                res += 1
                cur_max = next_max
            next_max = max(next_max, jump)
        return res

到了这里,关于leetcode - 1326. Minimum Number of Taps to Open to Water a Garden的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K

    Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K 1. 解题思路 2. 代码实现 题目链接:3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K 这一题我的思路上就是一个二分的思路,先确定一个上下界,然后不断通过二分来找到最大的price不超过k的值。 因此,剩下的

    2024年01月20日
    浏览(32)
  • LeetCode447. Number of Boomerangs

    You are given n points in the plane that are all distinct, where points[i] = [xi, yi]. A boomerang is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters). Return the number of boomerangs. Example 1: Input: points = [[0,0],[1,0],[2,0]] Output: 2 Explanation: The two boomerangs

    2024年02月02日
    浏览(25)
  • LeetCode 933. Number of Recent Calls

    ou have a  RecentCounter  class which counts the number of recent requests within a certain time frame. Implement the  RecentCounter  class: RecentCounter()  Initializes the counter with zero recent requests. int ping(int t)  Adds a new request at time  t , where  t  represents some time in milliseconds, and returns the number of requests that has h

    2024年02月08日
    浏览(36)
  • LeetCode //C - 933. Number of Recent Calls

    You have a RecentCounter class which counts the number of recent requests within a certain time frame. Implement the RecentCounter class: RecentCounter() Initializes the counter with zero recent requests. int ping(int t) Adds a new request at time t, where t represents some time in milliseconds, and returns the number of requests that has happened in the pas

    2024年01月23日
    浏览(37)
  • LeetCode每日一题(2457. Minimum Addition to Make Integer Beautiful)

    You are given two positive integers n and target. An integer is considered beautiful if the sum of its digits is less than or equal to target. Return the minimum non-negative integer x such that n + x is beautiful. The input will be generated such that it is always possible to make n beautiful. Example 1: Input: n = 16, target = 6 Output: 4 Explanation: Init

    2023年04月16日
    浏览(83)
  • LeetCode 1000. Minimum Cost to Merge Stones【记忆化搜索,动态规划,数组】困难

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2023年04月26日
    浏览(88)
  • LeetCode 1921. Eliminate Maximum Number of Monsters【贪心,计数排序】1527

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月09日
    浏览(39)
  • LeetCode2111. Minimum Operations to Make the Array K-Increasing——动态规划

    You are given a 0-indexed array arr consisting of n positive integers, and a positive integer k. The array arr is called K-increasing if arr[i-k] = arr[i] holds for every index i, where k = i = n-1. For example, arr = [4, 1, 5, 2, 6, 2] is K-increasing for k = 2 because: arr[0] = arr[2] (4 = 5) arr[1] = arr[3] (1 = 2) arr[2] = arr[4] (5 = 6) arr[3] = arr[5]

    2024年03月09日
    浏览(33)
  • Java异常 #Number of lines annotated by Git is not equal to number of lines in the file, check file …

    在项目中某个 java 文件左边栏右键查看代码版本履历(Annotate)时无法显示,IDEA 提示:Number of lines annotated by Git is not equal to number of lines in the file, check file encoding and line separators.   这个问题涉及到不同操作系统下文本文件的换行符差异引起的。在不同操作系统中,文本文件的

    2024年02月03日
    浏览(35)
  • LeetCode 2475. Number of Unequal Triplets in Array【数组,排序,哈希表】简单

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月09日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包