comments | difficulty | edit_url | rating | source | tags | ||||
---|---|---|---|---|---|---|---|---|---|
true |
Medium |
1975 |
Weekly Contest 359 Q4 |
|
You are given a 0-indexed integer array nums
and an integer k
.
A subarray is called equal if all of its elements are equal. Note that the empty subarray is an equal subarray.
Return the length of the longest possible equal subarray after deleting at most k
elements from nums
.
A subarray is a contiguous, possibly empty sequence of elements within an array.
Example 1:
Input: nums = [1,3,2,3,1,3], k = 3 Output: 3 Explanation: 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 = 3 with length equal to 3. It can be proven that no longer equal subarrays can be created.
Example 2:
Input: nums = [1,1,2,2,1,1], k = 2 Output: 4 Explanation: 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 is 4. It can be proven that no longer equal subarrays can be created.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= nums.length
0 <= k <= nums.length
We use two pointers to maintain a monotonically variable length window, and a hash table to maintain the number of occurrences of each element in the window.
The number of all elements in the window minus the number of the most frequently occurring element in the window is the number of elements that need to be deleted from the window.
Each time, we add the element pointed to by the right pointer to the window, then update the hash table, and also update the number of the most frequently occurring element in the window. When the number of elements that need to be deleted from the window exceeds
After the traversal, return the number of the most frequently occurring element.
The time complexity is
class Solution:
def longestEqualSubarray(self, nums: List[int], k: int) -> int:
cnt = Counter()
l = 0
mx = 0
for r, x in enumerate(nums):
cnt[x] += 1
mx = max(mx, cnt[x])
if r - l + 1 - mx > k:
cnt[nums[l]] -= 1
l += 1
return mx
class Solution {
public int longestEqualSubarray(List<Integer> nums, int k) {
Map<Integer, Integer> cnt = new HashMap<>();
int mx = 0, l = 0;
for (int r = 0; r < nums.size(); ++r) {
cnt.merge(nums.get(r), 1, Integer::sum);
mx = Math.max(mx, cnt.get(nums.get(r)));
if (r - l + 1 - mx > k) {
cnt.merge(nums.get(l++), -1, Integer::sum);
}
}
return mx;
}
}
class Solution {
public:
int longestEqualSubarray(vector<int>& nums, int k) {
unordered_map<int, int> cnt;
int mx = 0, l = 0;
for (int r = 0; r < nums.size(); ++r) {
mx = max(mx, ++cnt[nums[r]]);
if (r - l + 1 - mx > k) {
--cnt[nums[l++]];
}
}
return mx;
}
};
func longestEqualSubarray(nums []int, k int) int {
cnt := map[int]int{}
mx, l := 0, 0
for r, x := range nums {
cnt[x]++
mx = max(mx, cnt[x])
if r-l+1-mx > k {
cnt[nums[l]]--
l++
}
}
return mx
}
function longestEqualSubarray(nums: number[], k: number): number {
const cnt: Map<number, number> = new Map();
let mx = 0;
let l = 0;
for (let r = 0; r < nums.length; ++r) {
cnt.set(nums[r], (cnt.get(nums[r]) ?? 0) + 1);
mx = Math.max(mx, cnt.get(nums[r])!);
if (r - l + 1 - mx > k) {
cnt.set(nums[l], cnt.get(nums[l])! - 1);
++l;
}
}
return mx;
}
We can use a hash table
Next, we enumerate each element as the equal value element. We take out the index list
The time complexity is
class Solution:
def longestEqualSubarray(self, nums: List[int], k: int) -> int:
g = defaultdict(list)
for i, x in enumerate(nums):
g[x].append(i)
ans = 0
for ids in g.values():
l = 0
for r in range(len(ids)):
while ids[r] - ids[l] - (r - l) > k:
l += 1
ans = max(ans, r - l + 1)
return ans
class Solution {
public int longestEqualSubarray(List<Integer> nums, int k) {
int n = nums.size();
List<Integer>[] g = new List[n + 1];
Arrays.setAll(g, i -> new ArrayList<>());
for (int i = 0; i < n; ++i) {
g[nums.get(i)].add(i);
}
int ans = 0;
for (List<Integer> ids : g) {
int l = 0;
for (int r = 0; r < ids.size(); ++r) {
while (ids.get(r) - ids.get(l) - (r - l) > k) {
++l;
}
ans = Math.max(ans, r - l + 1);
}
}
return ans;
}
}
class Solution {
public:
int longestEqualSubarray(vector<int>& nums, int k) {
int n = nums.size();
vector<int> g[n + 1];
for (int i = 0; i < n; ++i) {
g[nums[i]].push_back(i);
}
int ans = 0;
for (const auto& ids : g) {
int l = 0;
for (int r = 0; r < ids.size(); ++r) {
while (ids[r] - ids[l] - (r - l) > k) {
++l;
}
ans = max(ans, r - l + 1);
}
}
return ans;
}
};
func longestEqualSubarray(nums []int, k int) (ans int) {
g := make([][]int, len(nums)+1)
for i, x := range nums {
g[x] = append(g[x], i)
}
for _, ids := range g {
l := 0
for r := range ids {
for ids[r]-ids[l]-(r-l) > k {
l++
}
ans = max(ans, r-l+1)
}
}
return
}
function longestEqualSubarray(nums: number[], k: number): number {
const n = nums.length;
const g: number[][] = Array.from({ length: n + 1 }, () => []);
for (let i = 0; i < n; ++i) {
g[nums[i]].push(i);
}
let ans = 0;
for (const ids of g) {
let l = 0;
for (let r = 0; r < ids.length; ++r) {
while (ids[r] - ids[l] - (r - l) > k) {
++l;
}
ans = Math.max(ans, r - l + 1);
}
}
return ans;
}