• 微信公众号：美女很有趣。 工作之余，放松一下，关注即送10G+美女照片！

# 1893. 检查是否区域内所有整数都被覆盖

3小时前 1次浏览

– 2 被第一个区间覆盖。
– 3 和 4 被第二个区间覆盖。
– 5 被第三个区间覆盖。

1 <= ranges.length <= 50
1 <= starti <= endi <= 50
1 <= left <= right <= 50

```import java.util.Arrays;

class Solution {
public boolean isCovered(int[][] ranges, int left, int right) {
// 思路1 : 排序  时间n*logn
// Arrays.sort(ranges, new Comparator<int[]>() {
//     public int compare(int[] o1, int[] o2) {
//         if(o1[0] == o2[0]) return o1[1] - o2[1];
//         else return o1[0] - o2[0];
//     }
// });

Arrays.sort(ranges, (o1, o2) -> {
if(o1[0] == o2[0]) return o1[1] - o2[1];
else return o1[0] - o2[0];
});

for(int[] r : ranges) {
if(left > right) return true;
if(r[0] <= left && left <= r[1]) left = r[1] + 1;
}

return left > right;
}

public boolean isCoveredTwo(int[][] ranges, int left, int right) {
// 思路2
int[] covered = new int[52];
for(int[] r : ranges) {
covered[r[0]]++;
covered[r[1] + 1]--;
}

// 这里更新数组 如果没有被覆盖到的位置更新完以后还会是0
for(int i = 1; i < 52; i++) {
covered[i] += covered[i - 1];
}

// 最后判断
for(int i = left; i <= right; i++) {
if(covered[i] == 0) return false;
}

return true;
}

public static void main(String[] args) {
int[][] ranges = {{1,2},{3,4},{5,6}};
int left = 2;
int right = 5;
System.out.println(new Solution().isCovered(ranges, left, right));
}
}```

View Code

```#include "vector"
#include "iostream"
using namespace std;

class Solution {
public:
bool isCovered(vector<vector<int>>& ranges, int left, int right) {
vector<int> diff(52, 0);   // 差分数组
for (auto&& range: ranges){
++diff[range[0]];
--diff[range[1]+1];
}
// 前缀和
int curr = 0;
for (int i = 1; i <= 50; ++i){
curr += diff[i];
if (i >= left && i <= right && curr <= 0){
return false;
}
}
return true;
}
};

int main() {
vector<vector<int>> ranges = {{1,2},{3,4},{5,6}};
int left = 2;
int right = 5;
cout << boolalpha << Solution().isCovered(ranges, left, right) << endl;
}```

View Code

```from typing import List

class Solution:
def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:
diff = [0] * 52   # 差分数组
for l, r in ranges:
diff[l] += 1
diff[r+1] -= 1
# 前缀和
curr = 0
for i in range(1, 51):
curr += diff[i]
if left <= i <= right and curr <= 0:
return False
return True

if __name__ == '__main__':
# ranges = [[1,2],[3,4],[5,6]]
# left, right = 2, 5
ranges = [[1,10],[10,20]]
left = 21
right = 21
print(Solution().isCovered(ranges, left, right))```

View Code