Skip to content

Commit

Permalink
leetcode: Add index to 1893
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Jul 29, 2024
1 parent a198d6b commit b946eaf
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@
- [1732. 找到最高海拔 Find the Highest Altitude](leetcode/1732.find-the-highest-altitude/index.md)
- [1780. 判断一个数字是否可以表示成三的幂的和 Check if Number is a Sum of Powers of Three](leetcode/1780.check-if-number-is-a-sum-of-powers-of-three/index.md)
- [1801-1900](leetcode/by-id/1801-1900.md)
- [1893. 检查是否区域内所有整数都被覆盖 Check if All the Integers in a Range Are Covered](leetcode/1893.check-if-all-the-integers-in-a-range-are-covered/index.md)
- [1901-2000](leetcode/by-id/1901-2000.md)
- [1991. 找到数组的中间位置 Find the Middle Index in Array](leetcode/1991.find-the-middle-index-in-array/index.md)
- [2101-2200](leetcode/by-id/2101-2200.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "lc-0000-name"
name = "lc-1893-check-if-all-the-integers-in-a-range-are-covered"
version = "0.1.0"
edition = "2021"
publish = false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,47 @@
# 1893. 检查是否区域内所有整数都被覆盖 Check if All the Integers in a Range Are Covered

#
[问题描述](https://leetcode.com/problems/check-if-all-the-integers-in-a-range-are-covered)

[问题描述](https://leetcode.com/problems/)
这个问题有两个思路可以处理.

## 集合 Set

使用集合来存储区间上的每个点.

步骤如下:

1. 创建集合
2. 遍历 `ranges` 数组, 将每个范围上的所有点位都存储到集合中
3. 遍历 `[left..=right]` 区间上的所有点位, 查看它们是否都在集合中

```rust
{{#include src/main.rs:5:24}}
```

该算法:

- 时间复杂度是 `O(n m)`, 其中 `n` 是范围的个数, 而 `m` 是最大的范围区间
- 空间复杂度是 `O(n)`, 其中 `n` 是范围包含的所有点位个数

## 合并区间 Merge intervals

这个方法用于计算区间重叠很方便.

其步骤如下:

1. 先对 `ranges` 数组进行排序, 依照范围的起始点
2. 构造合并区间 `intervals`
1. 初始化区间值 start = 0, end = 0
2. 遍历 ranges, 并判断当前区间是否能跟区间值 `[start..=end]` 拼接在一起, 判定条件是 `range[0] <= end + 1`
3. 如果可以, 就只需要移动区间的终点值, `end = end.max(range[1])`
4. 如果不行, 就先将当前区间 `[start..=end]` 加入到 `intervals`, 然后更新 `[start..=end]` 区间
3. 查找 `[left..=right]` 区间是否在 `intervals`

```rust
{{#include src/main.rs:26:61}}
```

该算法:

- 时间复杂度是 `O(n log(n))`, n 是 `ranges` 中的区间个数
- 空间复杂度是 `O(n)`, n 是 `ranges` 内不连接的区间个数
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ pub fn is_covered2(ranges: Vec<Vec<i32>>, left: i32, right: i32) -> bool {
let mut end = 0;
for range in ranges {
if range[0] > end + 1 {
// 区间没有重叠
// 区间无法拼接在一起
if start <= end {
intervals.push((start, end));
}
start = range[0];
end = range[1];
} else {
// 区间有重叠
// 区间可以拼接在一起
end = end.max(range[1]);
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/leetcode/tags/prefix-sum.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
3. [1422. 分割字符串的最大得分 Maximum Score After Splitting a String](../1422.maximum-score-after-splitting-a-string/index.md)
4. [1480. 一维数组的动态和 Running Sum of 1d Array](../1480.running-sum-of-1d-array/index.md)
5. [1732. 找到最高海拔 Find the Highest Altitude](../1732.find-the-highest-altitude/index.md)
6. [1991. 找到数组的中间位置 Find the Middle Index in Array](../1991.find-the-middle-index-in-array/index.md)
7. [2485. 找出中枢整数 Find the Pivot Integer](../2485.find-the-pivot-integer/index.md)
8. [2574. 左右元素和的差值 Left and Right Sum Differences](../2574.left-and-right-sum-differences/index.md)
9. [2848. 与车相交的点 Points That Intersect With Cars](../2848.points-that-intersect-with-cars/index.md)
10. [3028. 边界上的蚂蚁 Ant on the Boundary](../3028.ant-on-the-boundary/index.md)
6. [1893. 检查是否区域内所有整数都被覆盖 Check if All the Integers in a Range Are Covered](../1893.check-if-all-the-integers-in-a-range-are-covered/index.md)
7. [1991. 找到数组的中间位置 Find the Middle Index in Array](../1991.find-the-middle-index-in-array/index.md)
8. [2485. 找出中枢整数 Find the Pivot Integer](../2485.find-the-pivot-integer/index.md)
9. [2574. 左右元素和的差值 Left and Right Sum Differences](../2574.left-and-right-sum-differences/index.md)
10. [2848. 与车相交的点 Points That Intersect With Cars](../2848.points-that-intersect-with-cars/index.md)
11. [3028. 边界上的蚂蚁 Ant on the Boundary](../3028.ant-on-the-boundary/index.md)

## 中等

Expand Down

0 comments on commit b946eaf

Please sign in to comment.