Input: nums =[1,3,2,3,1,3], k =3Output: 3Explanation: It's optimal to delete the elements at index 2 and index 4.After deleting them, nums becomes equal to [1,3,3,3].The longest equal subarray starts at i =1 and ends at j =3with length equal to 3.It can be proven that no longer equal subarrays can be created.
Input: nums =[1,1,2,2,1,1], k =2Output: 4Explanation: It's optimal to delete the elements at index 2 and index 3.After deleting them, nums becomes equal to [1,1,1,1].The array itself is an equal subarray, so the answer is4.It can be proven that no longer equal subarrays can be created.
For each unique value, collect its indices. The problem reduces to finding the largest window of indices where the number of skipped indices (deletions) is at most k. This can be solved efficiently with a sliding window.
For each unique value in nums, collect all its indices in a list.
For each list, use two pointers (left, right) to find the largest window where the number of skipped indices (indices[right] - indices[left] - (right - left)) is at most k.
classSolution {
public:int longestEqualSubarray(vector<int>& nums, int k) {
unordered_map<int, vector<int>> pos;
for (int i =0; i < nums.size(); ++i) pos[nums[i]].push_back(i);
int ans =0;
for (auto& [v, idx] : pos) {
int l =0;
for (int r =0; r < idx.size(); ++r) {
while (idx[r] - idx[l] - (r - l) > k) ++l;
ans = max(ans, r - l +1);
}
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
funclongestEqualSubarray(nums []int, kint) int {
pos:=map[int][]int{}
fori, v:=rangenums {
pos[v] = append(pos[v], i)
}
ans:=0for_, idx:=rangepos {
l:=0forr:=0; r < len(idx); r++ {
foridx[r]-idx[l]-(r-l) > k {
l++ }
ifr-l+1 > ans {
ans = r-l+1 }
}
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
publicintlongestEqualSubarray(int[] nums, int k) {
Map<Integer, List<Integer>> pos =new HashMap<>();
for (int i = 0; i < nums.length; i++) pos.computeIfAbsent(nums[i], x ->new ArrayList<>()).add(i);
int ans = 0;
for (var idx : pos.values()) {
int l = 0;
for (int r = 0; r < idx.size(); r++) {
while (idx.get(r) - idx.get(l) - (r - l) > k) l++;
ans = Math.max(ans, r - l + 1);
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
funlongestEqualSubarray(nums: IntArray, k: Int): Int {
val pos = mutableMapOf<Int, MutableList<Int>>()
for (i in nums.indices) pos.getOrPut(nums[i]) { mutableListOf() }.add(i)
var ans = 0for (idx in pos.values) {
var l = 0for (r in idx.indices) {
while (idx[r] - idx[l] - (r - l) > k) l++ ans = maxOf(ans, r - l + 1)
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
deflongest_equal_subarray(nums: list[int], k: int) -> int:
from collections import defaultdict
pos: dict[int, list[int]] = defaultdict(list)
for i, v in enumerate(nums):
pos[v].append(i)
ans =0for idx in pos.values():
l =0for r in range(len(idx)):
while idx[r] - idx[l] - (r - l) > k:
l +=1 ans = max(ans, r - l +1)
return ans
impl Solution {
pubfnlongest_equal_subarray(nums: Vec<i32>, k: i32) -> i32 {
use std::collections::HashMap;
letmut pos: HashMap<i32, Vec<usize>>= HashMap::new();
for (i, &v) in nums.iter().enumerate() {
pos.entry(v).or_default().push(i);
}
letmut ans =0;
for idx in pos.values() {
letmut l =0;
for r in0..idx.len() {
while idx[r] - idx[l] - (r - l) > k asusize {
l +=1;
}
ans = ans.max(r - l +1);
}
}
ans asi32 }
}