Skip to content

Commit c07ca0b

Browse files
committed
feat: add solutions to lc problems: No.2655,3228
1 parent e980e6c commit c07ca0b

File tree

7 files changed

+193
-3
lines changed

7 files changed

+193
-3
lines changed

solution/2600-2699/2655.Find Maximal Uncovered Ranges/README.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,13 @@ tags:
8282

8383
### 方法一:排序
8484

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

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

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

9193
<!-- tabs:start -->
9294

@@ -177,6 +179,50 @@ func findMaximalUncoveredRanges(n int, ranges [][]int) (ans [][]int) {
177179
}
178180
```
179181

182+
#### TypeScript
183+
184+
```ts
185+
function findMaximalUncoveredRanges(n: number, ranges: number[][]): number[][] {
186+
ranges.sort((a, b) => a[0] - b[0]);
187+
let last = -1;
188+
const ans: number[][] = [];
189+
for (const [l, r] of ranges) {
190+
if (last + 1 < l) {
191+
ans.push([last + 1, l - 1]);
192+
}
193+
last = Math.max(last, r);
194+
}
195+
if (last + 1 < n) {
196+
ans.push([last + 1, n - 1]);
197+
}
198+
return ans;
199+
}
200+
```
201+
202+
#### Rust
203+
204+
```rust
205+
impl Solution {
206+
pub fn find_maximal_uncovered_ranges(n: i32, mut ranges: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
207+
ranges.sort_by_key(|x| x[0]);
208+
let mut last = -1;
209+
let mut ans = Vec::new();
210+
for range in ranges {
211+
let l = range[0];
212+
let r = range[1];
213+
if last + 1 < l {
214+
ans.push(vec![last + 1, l - 1]);
215+
}
216+
last = last.max(r);
217+
}
218+
if last + 1 < n {
219+
ans.push(vec![last + 1, n - 1]);
220+
}
221+
ans
222+
}
223+
}
224+
```
225+
180226
<!-- tabs:end -->
181227

182228
<!-- solution:end -->

solution/2600-2699/2655.Find Maximal Uncovered Ranges/README_EN.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,15 @@ tags:
7979

8080
<!-- solution:start -->
8181

82-
### Solution 1
82+
### Solution 1: Sorting
83+
84+
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$.
85+
86+
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.
87+
88+
Finally, we return the answer array.
89+
90+
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}$.
8391

8492
<!-- tabs:start -->
8593

@@ -170,6 +178,50 @@ func findMaximalUncoveredRanges(n int, ranges [][]int) (ans [][]int) {
170178
}
171179
```
172180

181+
#### TypeScript
182+
183+
```ts
184+
function findMaximalUncoveredRanges(n: number, ranges: number[][]): number[][] {
185+
ranges.sort((a, b) => a[0] - b[0]);
186+
let last = -1;
187+
const ans: number[][] = [];
188+
for (const [l, r] of ranges) {
189+
if (last + 1 < l) {
190+
ans.push([last + 1, l - 1]);
191+
}
192+
last = Math.max(last, r);
193+
}
194+
if (last + 1 < n) {
195+
ans.push([last + 1, n - 1]);
196+
}
197+
return ans;
198+
}
199+
```
200+
201+
#### Rust
202+
203+
```rust
204+
impl Solution {
205+
pub fn find_maximal_uncovered_ranges(n: i32, mut ranges: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
206+
ranges.sort_by_key(|x| x[0]);
207+
let mut last = -1;
208+
let mut ans = Vec::new();
209+
for range in ranges {
210+
let l = range[0];
211+
let r = range[1];
212+
if last + 1 < l {
213+
ans.push(vec![last + 1, l - 1]);
214+
}
215+
last = last.max(r);
216+
}
217+
if last + 1 < n {
218+
ans.push(vec![last + 1, n - 1]);
219+
}
220+
ans
221+
}
222+
}
223+
```
224+
173225
<!-- tabs:end -->
174226

175227
<!-- solution:end -->
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn find_maximal_uncovered_ranges(n: i32, mut ranges: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
3+
ranges.sort_by_key(|x| x[0]);
4+
let mut last = -1;
5+
let mut ans = Vec::new();
6+
for range in ranges {
7+
let l = range[0];
8+
let r = range[1];
9+
if last + 1 < l {
10+
ans.push(vec![last + 1, l - 1]);
11+
}
12+
last = last.max(r);
13+
}
14+
if last + 1 < n {
15+
ans.push(vec![last + 1, n - 1]);
16+
}
17+
ans
18+
}
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function findMaximalUncoveredRanges(n: number, ranges: number[][]): number[][] {
2+
ranges.sort((a, b) => a[0] - b[0]);
3+
let last = -1;
4+
const ans: number[][] = [];
5+
for (const [l, r] of ranges) {
6+
if (last + 1 < l) {
7+
ans.push([last + 1, l - 1]);
8+
}
9+
last = Math.max(last, r);
10+
}
11+
if (last + 1 < n) {
12+
ans.push([last + 1, n - 1]);
13+
}
14+
return ans;
15+
}

solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,27 @@ function maxOperations(s: string): number {
173173
}
174174
```
175175

176+
#### Rust
177+
178+
```rust
179+
impl Solution {
180+
pub fn max_operations(s: String) -> i32 {
181+
let mut ans = 0;
182+
let mut cnt = 0;
183+
let n = s.len();
184+
let bytes = s.as_bytes();
185+
for i in 0..n {
186+
if bytes[i] == b'1' {
187+
cnt += 1;
188+
} else if i > 0 && bytes[i - 1] == b'1' {
189+
ans += cnt;
190+
}
191+
}
192+
ans
193+
}
194+
}
195+
```
196+
176197
<!-- tabs:end -->
177198

178199
<!-- solution:end -->

solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README_EN.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,27 @@ function maxOperations(s: string): number {
171171
}
172172
```
173173

174+
#### Rust
175+
176+
```rust
177+
impl Solution {
178+
pub fn max_operations(s: String) -> i32 {
179+
let mut ans = 0;
180+
let mut cnt = 0;
181+
let n = s.len();
182+
let bytes = s.as_bytes();
183+
for i in 0..n {
184+
if bytes[i] == b'1' {
185+
cnt += 1;
186+
} else if i > 0 && bytes[i - 1] == b'1' {
187+
ans += cnt;
188+
}
189+
}
190+
ans
191+
}
192+
}
193+
```
194+
174195
<!-- tabs:end -->
175196

176197
<!-- solution:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn max_operations(s: String) -> i32 {
3+
let mut ans = 0;
4+
let mut cnt = 0;
5+
let n = s.len();
6+
let bytes = s.as_bytes();
7+
for i in 0..n {
8+
if bytes[i] == b'1' {
9+
cnt += 1;
10+
} else if i > 0 && bytes[i - 1] == b'1' {
11+
ans += cnt;
12+
}
13+
}
14+
ans
15+
}
16+
}

0 commit comments

Comments
 (0)