Skip to content

feat: add rust solutions to lc problem: No.1695 #4590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2025
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
70 changes: 62 additions & 8 deletions solution/1600-1699/1695.Maximum Erasure Value/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}$ 的长度。

<!-- tabs:start -->

Expand All @@ -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):
Expand Down Expand Up @@ -173,21 +173,48 @@ function maximumUniqueSubarray(nums: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn maximum_unique_subarray(nums: Vec<i32>) -> 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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 方法二:双指针
### 方法二:双指针(滑动窗口)

题目实际上是让我们找出一个最长的子数组,该子数组中所有元素都不相同。我们可以用两个指针 $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}$ 的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -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>) -> 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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
72 changes: 63 additions & 9 deletions solution/1600-1699/1695.Maximum Erasure Value/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}$.

<!-- tabs:start -->

Expand All @@ -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):
Expand Down Expand Up @@ -171,21 +171,48 @@ function maximumUniqueSubarray(nums: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn maximum_unique_subarray(nums: Vec<i32>) -> 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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 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}$.

<!-- tabs:start -->

Expand Down Expand Up @@ -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>) -> 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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
2 changes: 1 addition & 1 deletion solution/1600-1699/1695.Maximum Erasure Value/Solution.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
22 changes: 22 additions & 0 deletions solution/1600-1699/1695.Maximum Erasure Value/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
impl Solution {
pub fn maximum_unique_subarray(nums: Vec<i32>) -> 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
}
}
22 changes: 22 additions & 0 deletions solution/1600-1699/1695.Maximum Erasure Value/Solution2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::collections::HashSet;

impl Solution {
pub fn maximum_unique_subarray(nums: Vec<i32>) -> 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
}
}