Skip to content

Commit f4c6fcf

Browse files
authored
feat: add rust solutions to lc problem: No.1695 (#4590)
No.1695.Maximum Erasure Value
1 parent 60245f5 commit f4c6fcf

File tree

5 files changed

+170
-18
lines changed

5 files changed

+170
-18
lines changed

solution/1600-1699/1695.Maximum Erasure Value/README.md

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ tags:
6161

6262
### 方法一:数组或哈希表 + 前缀和
6363

64-
我们用数组或哈希表 $d$ 记录每个数字最后一次出现的位置,用 $s$ 记录前缀和,用 $j$ 记录当前不重复子数组的左端点。
64+
我们用数组或哈希表 $\text{d}$ 记录每个数字最后一次出现的位置,用前缀和数组 $\text{s}$ 记录从起点到当前位置的和。我们用变量 $j$ 记录当前不重复子数组的左端点。
6565

66-
遍历数组,对于每个数字 $v$,如果 $d[v]$ 存在,那么我们更新 $j$ 为 $max(j, d[v])$,这样就保证了当前不重复子数组不包含 $v$,然后更新答案为 $max(ans, s[i] - s[j])$,最后更新 $d[v]$ 为 $i$。
66+
遍历数组,对于每个数字 $v$,如果 $\text{d}[v]$ 存在,那么我们更新 $j$ 为 $\max(j, \text{d}[v])$,这样就保证了当前不重复子数组不包含 $v$。然后我们更新答案为 $\max(\text{ans}, \text{s}[i] - \text{s}[j])$,最后更新 $\text{d}[v]$ 为 $i$。
6767

68-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
68+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\text{nums}$ 的长度。
6969

7070
<!-- tabs:start -->
7171

@@ -74,7 +74,7 @@ tags:
7474
```python
7575
class Solution:
7676
def maximumUniqueSubarray(self, nums: List[int]) -> int:
77-
d = defaultdict(int)
77+
d = [0] * (max(nums) + 1)
7878
s = list(accumulate(nums, initial=0))
7979
ans = j = 0
8080
for i, v in enumerate(nums, 1):
@@ -173,21 +173,48 @@ function maximumUniqueSubarray(nums: number[]): number {
173173
}
174174
```
175175

176+
#### Rust
177+
178+
```rust
179+
impl Solution {
180+
pub fn maximum_unique_subarray(nums: Vec<i32>) -> i32 {
181+
let m = *nums.iter().max().unwrap() as usize;
182+
let mut d = vec![0; m + 1];
183+
let n = nums.len();
184+
185+
let mut s = vec![0; n + 1];
186+
for i in 0..n {
187+
s[i + 1] = s[i] + nums[i];
188+
}
189+
190+
let mut ans = 0;
191+
let mut j = 0;
192+
for (i, &v) in nums.iter().enumerate().map(|(i, v)| (i + 1, v)) {
193+
j = j.max(d[v as usize]);
194+
ans = ans.max(s[i] - s[j]);
195+
d[v as usize] = i;
196+
}
197+
198+
ans
199+
}
200+
}
201+
```
202+
176203
<!-- tabs:end -->
177204

178205
<!-- solution:end -->
179206

180207
<!-- solution:start -->
181208

182-
### 方法二:双指针
209+
### 方法二:双指针(滑动窗口)
183210

184-
题目实际上是让我们找出一个最长的子数组,该子数组中所有元素都不相同。我们可以用两个指针 $i$ 和 $j$ 分别指向子数组的左右边界,初始时 $i = 0$, $j = 0$。另外,我们用一个哈希表 $vis$ 记录子数组中的元素。
211+
题目实际上是让我们找出一个最长的子数组,该子数组中所有元素都不相同。我们可以用两个指针 $i$ 和 $j$ 分别指向子数组的左右边界,初始时 $i = 0$, $j = 0$。另外,我们用一个哈希表 $\text{vis}$ 记录子数组中的元素。
185212

186-
遍历数组,对于每个数字 $x$,如果 $x$ 在 $vis$ 中,那么我们不断地将 $nums[i]$ 从 $vis$ 中移除,直到 $x$ 不在 $vis$ 中为止。这样我们就找到了一个不包含重复元素的子数组。我们将 $x$ 加入 $vis$,并更新子数组的和 $s$,然后更新答案 $ans = \max(ans, s)$。
213+
遍历数组,对于每个数字 $x$,如果 $x$ 在 $\text{vis}$ 中,那么我们不断地将 $\text{nums}[i]$ 从 $\text{vis}$ 中移除,直到 $x$ 不在 $\text{vis}$ 中为止。这样我们就找到了一个不包含重复元素的子数组。我们将 $x$ 加入 $\text{vis}$,并更新子数组的和 $s$,然后更新答案 $\text{ans} = \max(\text{ans}, s)$。
187214

188215
遍历结束后,我们就可以得到最大的子数组和。
189216

190-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
217+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\text{nums}$ 的长度。
191218

192219
<!-- tabs:start -->
193220

@@ -292,6 +319,33 @@ function maximumUniqueSubarray(nums: number[]): number {
292319
}
293320
```
294321

322+
#### Rust
323+
324+
```rust
325+
use std::collections::HashSet;
326+
327+
impl Solution {
328+
pub fn maximum_unique_subarray(nums: Vec<i32>) -> i32 {
329+
let mut vis = HashSet::new();
330+
let (mut ans, mut s, mut i) = (0, 0, 0);
331+
332+
for &x in &nums {
333+
while vis.contains(&x) {
334+
let y = nums[i];
335+
s -= y;
336+
vis.remove(&y);
337+
i += 1;
338+
}
339+
vis.insert(x);
340+
s += x;
341+
ans = ans.max(s);
342+
}
343+
344+
ans
345+
}
346+
}
347+
```
348+
295349
<!-- tabs:end -->
296350

297351
<!-- solution:end -->

solution/1600-1699/1695.Maximum Erasure Value/README_EN.md

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ tags:
5959

6060
### Solution 1: Array or Hash Table + Prefix Sum
6161

62-
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.
62+
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.
6363

64-
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$.
64+
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$.
6565

66-
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.
66+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\text{nums}$.
6767

6868
<!-- tabs:start -->
6969

@@ -72,7 +72,7 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is
7272
```python
7373
class Solution:
7474
def maximumUniqueSubarray(self, nums: List[int]) -> int:
75-
d = defaultdict(int)
75+
d = [0] * (max(nums) + 1)
7676
s = list(accumulate(nums, initial=0))
7777
ans = j = 0
7878
for i, v in enumerate(nums, 1):
@@ -171,21 +171,48 @@ function maximumUniqueSubarray(nums: number[]): number {
171171
}
172172
```
173173

174+
#### Rust
175+
176+
```rust
177+
impl Solution {
178+
pub fn maximum_unique_subarray(nums: Vec<i32>) -> i32 {
179+
let m = *nums.iter().max().unwrap() as usize;
180+
let mut d = vec![0; m + 1];
181+
let n = nums.len();
182+
183+
let mut s = vec![0; n + 1];
184+
for i in 0..n {
185+
s[i + 1] = s[i] + nums[i];
186+
}
187+
188+
let mut ans = 0;
189+
let mut j = 0;
190+
for (i, &v) in nums.iter().enumerate().map(|(i, v)| (i + 1, v)) {
191+
j = j.max(d[v as usize]);
192+
ans = ans.max(s[i] - s[j]);
193+
d[v as usize] = i;
194+
}
195+
196+
ans
197+
}
198+
}
199+
```
200+
174201
<!-- tabs:end -->
175202

176203
<!-- solution:end -->
177204

178205
<!-- solution:start -->
179206

180-
### Solution 2: Two Pointers
207+
### Solution 2: Two Pointers (Sliding Window)
181208

182-
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.
209+
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.
183210

184-
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)$.
211+
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)$.
185212

186-
After the traversal, we can get the maximum sum of the subarray.
213+
After the iteration, we can get the maximum subarray sum.
187214

188-
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $nums$.
215+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\text{nums}$.
189216

190217
<!-- tabs:start -->
191218

@@ -290,6 +317,33 @@ function maximumUniqueSubarray(nums: number[]): number {
290317
}
291318
```
292319

320+
#### Rust
321+
322+
```rust
323+
use std::collections::HashSet;
324+
325+
impl Solution {
326+
pub fn maximum_unique_subarray(nums: Vec<i32>) -> i32 {
327+
let mut vis = HashSet::new();
328+
let (mut ans, mut s, mut i) = (0, 0, 0);
329+
330+
for &x in &nums {
331+
while vis.contains(&x) {
332+
let y = nums[i];
333+
s -= y;
334+
vis.remove(&y);
335+
i += 1;
336+
}
337+
vis.insert(x);
338+
s += x;
339+
ans = ans.max(s);
340+
}
341+
342+
ans
343+
}
344+
}
345+
```
346+
293347
<!-- tabs:end -->
294348

295349
<!-- solution:end -->

solution/1600-1699/1695.Maximum Erasure Value/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def maximumUniqueSubarray(self, nums: List[int]) -> int:
3-
d = defaultdict(int)
3+
d = [0] * (max(nums) + 1)
44
s = list(accumulate(nums, initial=0))
55
ans = j = 0
66
for i, v in enumerate(nums, 1):
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
impl Solution {
2+
pub fn maximum_unique_subarray(nums: Vec<i32>) -> i32 {
3+
let m = *nums.iter().max().unwrap() as usize;
4+
let mut d = vec![0; m + 1];
5+
let n = nums.len();
6+
7+
let mut s = vec![0; n + 1];
8+
for i in 0..n {
9+
s[i + 1] = s[i] + nums[i];
10+
}
11+
12+
let mut ans = 0;
13+
let mut j = 0;
14+
for (i, &v) in nums.iter().enumerate().map(|(i, v)| (i + 1, v)) {
15+
j = j.max(d[v as usize]);
16+
ans = ans.max(s[i] - s[j]);
17+
d[v as usize] = i;
18+
}
19+
20+
ans
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use std::collections::HashSet;
2+
3+
impl Solution {
4+
pub fn maximum_unique_subarray(nums: Vec<i32>) -> i32 {
5+
let mut vis = HashSet::new();
6+
let (mut ans, mut s, mut i) = (0, 0, 0);
7+
8+
for &x in &nums {
9+
while vis.contains(&x) {
10+
let y = nums[i];
11+
s -= y;
12+
vis.remove(&y);
13+
i += 1;
14+
}
15+
vis.insert(x);
16+
s += x;
17+
ans = ans.max(s);
18+
}
19+
20+
ans
21+
}
22+
}

0 commit comments

Comments
 (0)