Skip to content
Rain Hu's Workspace
Go back

[LeetCode] 2831. Find the Longest Equal Subarray

Rain Hu
class Solution {
public:
    int longestEqualSubarray(vector<int>& nums, int k) {
        int res = 0, left = 0, right = 0, n = nums.size();
        int maxcnt = 0;
        unordered_map<int,int> cnt;
        while (right < n) {
            int num = nums[right++];
            maxcnt = max(maxcnt, ++cnt[num]);
            while (right-left-maxcnt > k) cnt[nums[left++]]--;
            res = max(res, maxcnt);
        }
        return res;
    }
};

Share this post on:

Previous
[LeetCode] 2271. Maximum White Tiles Covered by a Carpet
Next
[LeetCode] 2516. Take K of Each Character From Left and Right