LeetCode 2958: Length of Longest Subarray With at Most K Frequency Solution
Master LeetCode problem 2958 (Length of Longest Subarray With at Most K Frequency), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2958. Length of Longest Subarray With at Most K Frequency
Problem Explanation
Explanation
To solve this problem, we can use a sliding window approach. We will iterate over the array while maintaining a window that contains elements with frequencies less than or equal to k. We will use a hashmap to keep track of the frequency of each element within the current window. If at any point an element's frequency exceeds k, we will shrink the window from the left side until all elements within the window satisfy the frequency constraint. We will update the maximum length of the good subarray as we iterate through the array.
- Initialize variables:
leftandrightpointers to define the window boundaries,maxFreqto store the maximum frequency allowed,freqMapto store element frequencies. - Iterate over the array:
- Update the frequency of the current element in
freqMapand calculate the maximum frequency encountered. - If any element within the window exceeds
k, shrink the window from the left. - Update the maximum length of the good subarray.
- Update the frequency of the current element in
- Return the maximum length of the good subarray.
Time complexity: O(n) where n is the number of elements in the input array. Space complexity: O(n) for the hashmap.
Solution Code
class Solution {
public int longestSubarray(int[] nums, int k) {
int left = 0, right = 0;
int maxFreq = 0;
Map<Integer, Integer> freqMap = new HashMap<>();
int maxLength = 0;
while (right < nums.length) {
int num = nums[right++];
freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
maxFreq = Math.max(maxFreq, freqMap.get(num));
while (right - left - maxFreq > k) {
int leftNum = nums[left++];
freqMap.put(leftNum, freqMap.get(leftNum) - 1);
if (freqMap.get(leftNum) == 0) {
freqMap.remove(leftNum);
}
}
maxLength = Math.max(maxLength, right - left);
}
return maxLength;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2958 (Length of Longest Subarray With at Most K Frequency)?
This page provides optimized solutions for LeetCode problem 2958 (Length of Longest Subarray With at Most K Frequency) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2958 (Length of Longest Subarray With at Most K Frequency)?
The time complexity for LeetCode 2958 (Length of Longest Subarray With at Most K Frequency) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2958 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2958 in Java, C++, or Python.