From 7892a6790df30a4a82d43fd95706d5d55c6bd0da Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 22 Jul 2025 07:09:57 +0800 Subject: [PATCH] feat: add rust solutions to lc problem: No.1695 No.1695.Maximum Erasure Value --- .../1695.Maximum Erasure Value/README.md | 70 +++++++++++++++--- .../1695.Maximum Erasure Value/README_EN.md | 72 ++++++++++++++++--- .../1695.Maximum Erasure Value/Solution.py | 2 +- .../1695.Maximum Erasure Value/Solution.rs | 22 ++++++ .../1695.Maximum Erasure Value/Solution2.rs | 22 ++++++ 5 files changed, 170 insertions(+), 18 deletions(-) create mode 100644 solution/1600-1699/1695.Maximum Erasure Value/Solution.rs create mode 100644 solution/1600-1699/1695.Maximum Erasure Value/Solution2.rs diff --git a/solution/1600-1699/1695.Maximum Erasure Value/README.md b/solution/1600-1699/1695.Maximum Erasure Value/README.md index 890ed06b4617a..e3acd494c64a7 100644 --- a/solution/1600-1699/1695.Maximum Erasure Value/README.md +++ b/solution/1600-1699/1695.Maximum Erasure Value/README.md @@ -61,11 +61,11 @@ tags: ### 方法一:数组或哈希表 + 前缀和 -我们用数组或哈希表 $d$ 记录每个数字最后一次出现的位置,用 $s$ 记录前缀和,用 $j$ 记录当前不重复子数组的左端点。 +我们用数组或哈希表 $\text{d}$ 记录每个数字最后一次出现的位置,用前缀和数组 $\text{s}$ 记录从起点到当前位置的和。我们用变量 $j$ 记录当前不重复子数组的左端点。 -遍历数组,对于每个数字 $v$,如果 $d[v]$ 存在,那么我们更新 $j$ 为 $max(j, d[v])$,这样就保证了当前不重复子数组不包含 $v$,然后更新答案为 $max(ans, s[i] - s[j])$,最后更新 $d[v]$ 为 $i$。 +遍历数组,对于每个数字 $v$,如果 $\text{d}[v]$ 存在,那么我们更新 $j$ 为 $\max(j, \text{d}[v])$,这样就保证了当前不重复子数组不包含 $v$。然后我们更新答案为 $\max(\text{ans}, \text{s}[i] - \text{s}[j])$,最后更新 $\text{d}[v]$ 为 $i$。 -时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。 +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\text{nums}$ 的长度。 @@ -74,7 +74,7 @@ tags: ```python class Solution: def maximumUniqueSubarray(self, nums: List[int]) -> int: - d = defaultdict(int) + d = [0] * (max(nums) + 1) s = list(accumulate(nums, initial=0)) ans = j = 0 for i, v in enumerate(nums, 1): @@ -173,21 +173,48 @@ function maximumUniqueSubarray(nums: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn maximum_unique_subarray(nums: Vec) -> i32 { + let m = *nums.iter().max().unwrap() as usize; + let mut d = vec![0; m + 1]; + let n = nums.len(); + + let mut s = vec![0; n + 1]; + for i in 0..n { + s[i + 1] = s[i] + nums[i]; + } + + let mut ans = 0; + let mut j = 0; + for (i, &v) in nums.iter().enumerate().map(|(i, v)| (i + 1, v)) { + j = j.max(d[v as usize]); + ans = ans.max(s[i] - s[j]); + d[v as usize] = i; + } + + ans + } +} +``` + -### 方法二:双指针 +### 方法二:双指针(滑动窗口) -题目实际上是让我们找出一个最长的子数组,该子数组中所有元素都不相同。我们可以用两个指针 $i$ 和 $j$ 分别指向子数组的左右边界,初始时 $i = 0$, $j = 0$。另外,我们用一个哈希表 $vis$ 记录子数组中的元素。 +题目实际上是让我们找出一个最长的子数组,该子数组中所有元素都不相同。我们可以用两个指针 $i$ 和 $j$ 分别指向子数组的左右边界,初始时 $i = 0$, $j = 0$。另外,我们用一个哈希表 $\text{vis}$ 记录子数组中的元素。 -遍历数组,对于每个数字 $x$,如果 $x$ 在 $vis$ 中,那么我们不断地将 $nums[i]$ 从 $vis$ 中移除,直到 $x$ 不在 $vis$ 中为止。这样我们就找到了一个不包含重复元素的子数组。我们将 $x$ 加入 $vis$,并更新子数组的和 $s$,然后更新答案 $ans = \max(ans, s)$。 +遍历数组,对于每个数字 $x$,如果 $x$ 在 $\text{vis}$ 中,那么我们不断地将 $\text{nums}[i]$ 从 $\text{vis}$ 中移除,直到 $x$ 不在 $\text{vis}$ 中为止。这样我们就找到了一个不包含重复元素的子数组。我们将 $x$ 加入 $\text{vis}$,并更新子数组的和 $s$,然后更新答案 $\text{ans} = \max(\text{ans}, s)$。 遍历结束后,我们就可以得到最大的子数组和。 -时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。 +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\text{nums}$ 的长度。 @@ -292,6 +319,33 @@ function maximumUniqueSubarray(nums: number[]): number { } ``` +#### Rust + +```rust +use std::collections::HashSet; + +impl Solution { + pub fn maximum_unique_subarray(nums: Vec) -> i32 { + let mut vis = HashSet::new(); + let (mut ans, mut s, mut i) = (0, 0, 0); + + for &x in &nums { + while vis.contains(&x) { + let y = nums[i]; + s -= y; + vis.remove(&y); + i += 1; + } + vis.insert(x); + s += x; + ans = ans.max(s); + } + + ans + } +} +``` + diff --git a/solution/1600-1699/1695.Maximum Erasure Value/README_EN.md b/solution/1600-1699/1695.Maximum Erasure Value/README_EN.md index 4a0e89c95882b..cc20b0f8b6cfe 100644 --- a/solution/1600-1699/1695.Maximum Erasure Value/README_EN.md +++ b/solution/1600-1699/1695.Maximum Erasure Value/README_EN.md @@ -59,11 +59,11 @@ tags: ### Solution 1: Array or Hash Table + Prefix Sum -We use an array or hash table $d$ to record the last occurrence of each number, use $s$ to record the prefix sum, and use $j$ to record the left endpoint of the current non-repeating subarray. +We use an array or hash table $\text{d}$ to record the last occurrence position of each number, and use a prefix sum array $\text{s}$ to record the sum from the starting point to the current position. We use a variable $j$ to record the left endpoint of the current non-repeating subarray. -We traverse the array, for each number $v$, if $d[v]$ exists, then we update $j$ to $max(j, d[v])$, which ensures that the current non-repeating subarray does not contain $v$. Then we update the answer to $max(ans, s[i] - s[j])$, and finally update $d[v]$ to $i$. +We iterate through the array. For each number $v$, if $\text{d}[v]$ exists, we update $j$ to $\max(j, \text{d}[v])$, which ensures that the current non-repeating subarray does not contain $v$. Then we update the answer to $\max(\text{ans}, \text{s}[i] - \text{s}[j])$, and finally update $\text{d}[v]$ to $i$. -The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$. +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\text{nums}$. @@ -72,7 +72,7 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is ```python class Solution: def maximumUniqueSubarray(self, nums: List[int]) -> int: - d = defaultdict(int) + d = [0] * (max(nums) + 1) s = list(accumulate(nums, initial=0)) ans = j = 0 for i, v in enumerate(nums, 1): @@ -171,21 +171,48 @@ function maximumUniqueSubarray(nums: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn maximum_unique_subarray(nums: Vec) -> i32 { + let m = *nums.iter().max().unwrap() as usize; + let mut d = vec![0; m + 1]; + let n = nums.len(); + + let mut s = vec![0; n + 1]; + for i in 0..n { + s[i + 1] = s[i] + nums[i]; + } + + let mut ans = 0; + let mut j = 0; + for (i, &v) in nums.iter().enumerate().map(|(i, v)| (i + 1, v)) { + j = j.max(d[v as usize]); + ans = ans.max(s[i] - s[j]); + d[v as usize] = i; + } + + ans + } +} +``` + -### Solution 2: Two Pointers +### Solution 2: Two Pointers (Sliding Window) -The problem is actually asking us to find the longest subarray in which all elements are distinct. We can use two pointers $i$ and $j$ to point to the left and right boundaries of the subarray, initially $i = 0$, $j = 0$. In addition, we use a hash table $vis$ to record the elements in the subarray. +The problem is essentially asking us to find the longest subarray where all elements are distinct. We can use two pointers $i$ and $j$ to point to the left and right boundaries of the subarray, initially $i = 0$ and $j = 0$. Additionally, we use a hash table $\text{vis}$ to record the elements in the subarray. -We traverse the array, for each number $x$, if $x$ is in $vis$, then we continuously remove $nums[i]$ from $vis$, until $x$ is not in $vis$. In this way, we find a subarray without duplicate elements. We add $x$ to $vis$, update the sum of the subarray $s$, and then update the answer $ans = \max(ans, s)$. +We iterate through the array. For each number $x$, if $x$ is in $\text{vis}$, we continuously remove $\text{nums}[i]$ from $\text{vis}$ until $x$ is no longer in $\text{vis}$. This way, we find a subarray that contains no duplicate elements. We add $x$ to $\text{vis}$, update the subarray sum $s$, and then update the answer $\text{ans} = \max(\text{ans}, s)$. -After the traversal, we can get the maximum sum of the subarray. +After the iteration, we can get the maximum subarray sum. -The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $nums$. +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\text{nums}$. @@ -290,6 +317,33 @@ function maximumUniqueSubarray(nums: number[]): number { } ``` +#### Rust + +```rust +use std::collections::HashSet; + +impl Solution { + pub fn maximum_unique_subarray(nums: Vec) -> i32 { + let mut vis = HashSet::new(); + let (mut ans, mut s, mut i) = (0, 0, 0); + + for &x in &nums { + while vis.contains(&x) { + let y = nums[i]; + s -= y; + vis.remove(&y); + i += 1; + } + vis.insert(x); + s += x; + ans = ans.max(s); + } + + ans + } +} +``` + diff --git a/solution/1600-1699/1695.Maximum Erasure Value/Solution.py b/solution/1600-1699/1695.Maximum Erasure Value/Solution.py index 07b780f3444e4..068e35bfd907b 100644 --- a/solution/1600-1699/1695.Maximum Erasure Value/Solution.py +++ b/solution/1600-1699/1695.Maximum Erasure Value/Solution.py @@ -1,6 +1,6 @@ class Solution: def maximumUniqueSubarray(self, nums: List[int]) -> int: - d = defaultdict(int) + d = [0] * (max(nums) + 1) s = list(accumulate(nums, initial=0)) ans = j = 0 for i, v in enumerate(nums, 1): diff --git a/solution/1600-1699/1695.Maximum Erasure Value/Solution.rs b/solution/1600-1699/1695.Maximum Erasure Value/Solution.rs new file mode 100644 index 0000000000000..f9212293a0775 --- /dev/null +++ b/solution/1600-1699/1695.Maximum Erasure Value/Solution.rs @@ -0,0 +1,22 @@ +impl Solution { + pub fn maximum_unique_subarray(nums: Vec) -> i32 { + let m = *nums.iter().max().unwrap() as usize; + let mut d = vec![0; m + 1]; + let n = nums.len(); + + let mut s = vec![0; n + 1]; + for i in 0..n { + s[i + 1] = s[i] + nums[i]; + } + + let mut ans = 0; + let mut j = 0; + for (i, &v) in nums.iter().enumerate().map(|(i, v)| (i + 1, v)) { + j = j.max(d[v as usize]); + ans = ans.max(s[i] - s[j]); + d[v as usize] = i; + } + + ans + } +} diff --git a/solution/1600-1699/1695.Maximum Erasure Value/Solution2.rs b/solution/1600-1699/1695.Maximum Erasure Value/Solution2.rs new file mode 100644 index 0000000000000..fb90a2d2bb733 --- /dev/null +++ b/solution/1600-1699/1695.Maximum Erasure Value/Solution2.rs @@ -0,0 +1,22 @@ +use std::collections::HashSet; + +impl Solution { + pub fn maximum_unique_subarray(nums: Vec) -> i32 { + let mut vis = HashSet::new(); + let (mut ans, mut s, mut i) = (0, 0, 0); + + for &x in &nums { + while vis.contains(&x) { + let y = nums[i]; + s -= y; + vis.remove(&y); + i += 1; + } + vis.insert(x); + s += x; + ans = ans.max(s); + } + + ans + } +}