From c07ca0bb533f82c9d195d9e27493e1139e9a0397 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sun, 9 Nov 2025 21:47:25 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.2655,3228 --- .../README.md | 50 ++++++++++++++++- .../README_EN.md | 54 ++++++++++++++++++- .../Solution.rs | 19 +++++++ .../Solution.ts | 15 ++++++ .../README.md | 21 ++++++++ .../README_EN.md | 21 ++++++++ .../Solution.rs | 16 ++++++ 7 files changed, 193 insertions(+), 3 deletions(-) create mode 100644 solution/2600-2699/2655.Find Maximal Uncovered Ranges/Solution.rs create mode 100644 solution/2600-2699/2655.Find Maximal Uncovered Ranges/Solution.ts create mode 100644 solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/Solution.rs diff --git a/solution/2600-2699/2655.Find Maximal Uncovered Ranges/README.md b/solution/2600-2699/2655.Find Maximal Uncovered Ranges/README.md index 068b97848071c..f3dff15a73b89 100644 --- a/solution/2600-2699/2655.Find Maximal Uncovered Ranges/README.md +++ b/solution/2600-2699/2655.Find Maximal Uncovered Ranges/README.md @@ -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}$ 的长度。 @@ -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> { + 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 + } +} +``` + diff --git a/solution/2600-2699/2655.Find Maximal Uncovered Ranges/README_EN.md b/solution/2600-2699/2655.Find Maximal Uncovered Ranges/README_EN.md index 48e350fc88fb0..3f5126c54d0ee 100644 --- a/solution/2600-2699/2655.Find Maximal Uncovered Ranges/README_EN.md +++ b/solution/2600-2699/2655.Find Maximal Uncovered Ranges/README_EN.md @@ -79,7 +79,15 @@ tags: -### 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}$. @@ -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> { + 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 + } +} +``` + diff --git a/solution/2600-2699/2655.Find Maximal Uncovered Ranges/Solution.rs b/solution/2600-2699/2655.Find Maximal Uncovered Ranges/Solution.rs new file mode 100644 index 0000000000000..d087885421073 --- /dev/null +++ b/solution/2600-2699/2655.Find Maximal Uncovered Ranges/Solution.rs @@ -0,0 +1,19 @@ +impl Solution { + pub fn find_maximal_uncovered_ranges(n: i32, mut ranges: Vec>) -> Vec> { + 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 + } +} diff --git a/solution/2600-2699/2655.Find Maximal Uncovered Ranges/Solution.ts b/solution/2600-2699/2655.Find Maximal Uncovered Ranges/Solution.ts new file mode 100644 index 0000000000000..df098f52a3bf2 --- /dev/null +++ b/solution/2600-2699/2655.Find Maximal Uncovered Ranges/Solution.ts @@ -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; +} diff --git a/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README.md b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README.md index 2ab30c0cf52c6..6be43af262626 100644 --- a/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README.md +++ b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README.md @@ -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 + } +} +``` + diff --git a/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README_EN.md b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README_EN.md index 2f1b503b2180f..e7797875746bf 100644 --- a/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README_EN.md +++ b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README_EN.md @@ -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 + } +} +``` + diff --git a/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/Solution.rs b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/Solution.rs new file mode 100644 index 0000000000000..1973b6de9e300 --- /dev/null +++ b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/Solution.rs @@ -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 + } +}