You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/1600-1699/1695.Maximum Erasure Value/README_EN.md
+63-9Lines changed: 63 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -59,11 +59,11 @@ tags:
59
59
60
60
### Solution 1: Array or Hash Table + Prefix Sum
61
61
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.
63
63
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$.
65
65
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}$.
67
67
68
68
<!-- tabs:start -->
69
69
@@ -72,7 +72,7 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is
for (i, &v) innums.iter().enumerate().map(|(i, v)| (i+1, v)) {
191
+
j=j.max(d[vasusize]);
192
+
ans=ans.max(s[i] -s[j]);
193
+
d[vasusize] =i;
194
+
}
195
+
196
+
ans
197
+
}
198
+
}
199
+
```
200
+
174
201
<!-- tabs:end -->
175
202
176
203
<!-- solution:end -->
177
204
178
205
<!-- solution:start -->
179
206
180
-
### Solution 2: Two Pointers
207
+
### Solution 2: Two Pointers (Sliding Window)
181
208
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.
183
210
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)$.
185
212
186
-
After the traversal, we can get the maximum sum of the subarray.
213
+
After the iteration, we can get the maximum subarray sum.
187
214
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}$.
189
216
190
217
<!-- tabs:start -->
191
218
@@ -290,6 +317,33 @@ function maximumUniqueSubarray(nums: number[]): number {
0 commit comments