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/2800-2899/2831.Find the Longest Equal Subarray/README_EN.md
+130
Original file line number
Diff line number
Diff line change
@@ -177,4 +177,134 @@ function longestEqualSubarray(nums: number[], k: number): number {
177
177
178
178
<!-- solution:end -->
179
179
180
+
<!-- source:start -->
181
+
182
+
### Solution 2: Hash Table + Two Pointers (Method 2)
183
+
184
+
We can use a hash table $g$ to maintain the index list of each element.
185
+
186
+
Next, we enumerate each element as the equal value element. We take out the index list $ids$ of this element from the hash table $g$. Then we define two pointers $l$ and $r$ to maintain a window, so that the number of elements in the window minus the number of equal value elements does not exceed $k$. Therefore, we only need to find the largest window that meets the condition.
187
+
188
+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array.
0 commit comments