Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions solution/2600-2699/2655.Find Maximal Uncovered Ranges/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ tags:

### 方法一:排序

我们将所有的区间按照左端点从小到大排序,然后从左到右遍历所有的区间,维护一个变量 $last$ 表示当前已经被覆盖的最右端点,初始时 $last=-1$。如果当前区间的左端点大于 $last+1$,那么说明 $[last+1, l-1]$ 是一个未被覆盖的区间,我们将其加入答案数组中。然后我们更新 $last$ 为当前区间的右端点,继续遍历下一个区间。当遍历完所有的区间后,如果 $last+1 \lt n$,那么说明 $[last+1, n-1]$ 是一个未被覆盖的区间,我们将其加入答案数组中。
我们将所有的区间按照左端点从小到大排序,然后从左到右遍历所有的区间,维护一个变量 $\textit{last}$ 表示当前已经被覆盖的最右端点,初始时 $\textit{last}=-1$。

如果当前区间的左端点大于 $\textit{last}+1$,那么说明 $[\textit{last}+1, l-1]$ 是一个未被覆盖的区间,我们将其加入答案数组中。然后我们更新 $\textit{last}$ 为当前区间的右端点,继续遍历下一个区间。当遍历完所有的区间后,如果 $\textit{last}+1 < n$,那么说明 $[\textit{last}+1, n-1]$ 是一个未被覆盖的区间,我们将其加入答案数组中。

最后我们将答案数组返回即可。

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 $ranges$ 的长度。
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 $\textit{ranges}$ 的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -177,6 +179,50 @@ func findMaximalUncoveredRanges(n int, ranges [][]int) (ans [][]int) {
}
```

#### TypeScript

```ts
function findMaximalUncoveredRanges(n: number, ranges: number[][]): number[][] {
ranges.sort((a, b) => a[0] - b[0]);
let last = -1;
const ans: number[][] = [];
for (const [l, r] of ranges) {
if (last + 1 < l) {
ans.push([last + 1, l - 1]);
}
last = Math.max(last, r);
}
if (last + 1 < n) {
ans.push([last + 1, n - 1]);
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn find_maximal_uncovered_ranges(n: i32, mut ranges: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
ranges.sort_by_key(|x| x[0]);
let mut last = -1;
let mut ans = Vec::new();
for range in ranges {
let l = range[0];
let r = range[1];
if last + 1 < l {
ans.push(vec![last + 1, l - 1]);
}
last = last.max(r);
}
if last + 1 < n {
ans.push(vec![last + 1, n - 1]);
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,15 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Sorting

We sort all intervals by their left endpoints in ascending order, then traverse all intervals from left to right, maintaining a variable $\textit{last}$ to represent the rightmost endpoint that has been covered so far, initially $\textit{last}=-1$.

If the left endpoint of the current interval is greater than $\textit{last}+1$, it means $[\textit{last}+1, l-1]$ is an uncovered interval, and we add it to the answer array. Then we update $\textit{last}$ to the right endpoint of the current interval and continue traversing the next interval. After traversing all intervals, if $\textit{last}+1 < n$, it means $[\textit{last}+1, n-1]$ is an uncovered interval, and we add it to the answer array.

Finally, we return the answer array.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$, where $n$ is the length of the array $\textit{ranges}$.

<!-- tabs:start -->

Expand Down Expand Up @@ -170,6 +178,50 @@ func findMaximalUncoveredRanges(n int, ranges [][]int) (ans [][]int) {
}
```

#### TypeScript

```ts
function findMaximalUncoveredRanges(n: number, ranges: number[][]): number[][] {
ranges.sort((a, b) => a[0] - b[0]);
let last = -1;
const ans: number[][] = [];
for (const [l, r] of ranges) {
if (last + 1 < l) {
ans.push([last + 1, l - 1]);
}
last = Math.max(last, r);
}
if (last + 1 < n) {
ans.push([last + 1, n - 1]);
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn find_maximal_uncovered_ranges(n: i32, mut ranges: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
ranges.sort_by_key(|x| x[0]);
let mut last = -1;
let mut ans = Vec::new();
for range in ranges {
let l = range[0];
let r = range[1];
if last + 1 < l {
ans.push(vec![last + 1, l - 1]);
}
last = last.max(r);
}
if last + 1 < n {
ans.push(vec![last + 1, n - 1]);
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
19 changes: 19 additions & 0 deletions solution/2600-2699/2655.Find Maximal Uncovered Ranges/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
impl Solution {
pub fn find_maximal_uncovered_ranges(n: i32, mut ranges: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
ranges.sort_by_key(|x| x[0]);
let mut last = -1;
let mut ans = Vec::new();
for range in ranges {
let l = range[0];
let r = range[1];
if last + 1 < l {
ans.push(vec![last + 1, l - 1]);
}
last = last.max(r);
}
if last + 1 < n {
ans.push(vec![last + 1, n - 1]);
}
ans
}
}
15 changes: 15 additions & 0 deletions solution/2600-2699/2655.Find Maximal Uncovered Ranges/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function findMaximalUncoveredRanges(n: number, ranges: number[][]): number[][] {
ranges.sort((a, b) => a[0] - b[0]);
let last = -1;
const ans: number[][] = [];
for (const [l, r] of ranges) {
if (last + 1 < l) {
ans.push([last + 1, l - 1]);
}
last = Math.max(last, r);
}
if (last + 1 < n) {
ans.push([last + 1, n - 1]);
}
return ans;
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,27 @@ function maxOperations(s: string): number {
}
```

#### Rust

```rust
impl Solution {
pub fn max_operations(s: String) -> i32 {
let mut ans = 0;
let mut cnt = 0;
let n = s.len();
let bytes = s.as_bytes();
for i in 0..n {
if bytes[i] == b'1' {
cnt += 1;
} else if i > 0 && bytes[i - 1] == b'1' {
ans += cnt;
}
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,27 @@ function maxOperations(s: string): number {
}
```

#### Rust

```rust
impl Solution {
pub fn max_operations(s: String) -> i32 {
let mut ans = 0;
let mut cnt = 0;
let n = s.len();
let bytes = s.as_bytes();
for i in 0..n {
if bytes[i] == b'1' {
cnt += 1;
} else if i > 0 && bytes[i - 1] == b'1' {
ans += cnt;
}
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
impl Solution {
pub fn max_operations(s: String) -> i32 {
let mut ans = 0;
let mut cnt = 0;
let n = s.len();
let bytes = s.as_bytes();
for i in 0..n {
if bytes[i] == b'1' {
cnt += 1;
} else if i > 0 && bytes[i - 1] == b'1' {
ans += cnt;
}
}
ans
}
}