leetcode 1893. Check if All the Integers in a Range Are Covered(python)

文章讨论了一个编程问题,给定一个二维整数数组ranges和两个整数left和right,目标是检查[left,right]范围内的每个整数是否至少由数组中的一个区间完全覆盖。提供了两种解决方案,一种是逐个遍历比较,另一种是利用集合交集来优化查找过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

描述

You are given a 2D integer array ranges and two integers left and right. Each ranges[i] = [starti, endi] represents an inclusive interval between starti and endi.

Return true if each integer in the inclusive range [left, right] is covered by at least one interval in ranges. Return false otherwise.

An integer x is covered by an interval ranges[i] = [starti, endi] if starti <= x <= endi.

Example 1:

Input: ranges = [[1,2],[3,4],[5,6]], left = 2, right = 5
Output: true
Explanation: Every integer between 2 and 5 is covered:
- 2 is covered by the first range.
- 3 and 4 are covered by the second range.
- 5 is covered by the third range.

Example 2:

Input: ranges = [[1,10],[10,20]], left = 21, right = 21
Output: false
Explanation: 21 is not covered by any range.

Note:

1 <= ranges.length <= 50

1 <= starti <= endi <= 50

1 <= left <= right <= 50

解析

根据题意,就是给出一个列表 ranges ,里面都是一个个范围,判断 [left, right] 中的每个数字至少都被一个 range 所包含,如果 [left, right] 中的每个数字都至少被一个范围覆盖直接返回 True , 否则返回 False 。思路简单,直接看代码 。

解答

class Solution(object):
    def isCovered(self, ranges, left, right):
        """
        :type ranges: List[List[int]]
        :type left: int
        :type right: int
        :rtype: bool
        """
        for i in range(left, right + 1):
            count = 0
            for r in ranges:
                count += 1
                if r[0] <= i <= r[1]:
                    break
                elif count==len(ranges):
                    return False
        return True

运行结果

Runtime: 28 ms, faster than 75.57% of Python online submissions for Check if All the Integers in a Range Are Covered.
Memory Usage: 13.2 MB, less than 89.31% of Python online submissions for Check if All the Integers in a Range Are Covered.

解析

另外可以将 ranges 表示的范围中的数字都放入一个集合 a 中,将 [left,right] 中的数字放入一个集合 b 中,然后求两个集合中间的交集是否等于 b ,如果等于直接返回 True ,否则返回 False 。

解答

class Solution(object):
    def isCovered(self, ranges, left, right):
        """
        :type ranges: List[List[int]]
        :type left: int
        :type right: int
        :rtype: bool
        """
        a = set(x for x,y in ranges for x in range(x,y+1))
        b = set(x for x in range(left, right+1))
        return a.intersection(b)==b

运行结果

Runtime: 28 ms, faster than 58.93% of Python online submissions for Check if All the Integers in a Range Are Covered.
Memory Usage: 13.7 MB, less than 7.74% of Python online submissions for Check if All the Integers in a Range Are Covered.

原题链接:https://leetcode.com/problems/check-if-all-the-integers-in-a-range-are-covered

您的支持是我最大的动力

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王大丫丫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值