2958. Length of Longest Subarray With at Most K Frequency
This is a very common subarray technique, remember since it’s a subarray it has to be a sliding window. If it were a , it becomes a DP or greedy solution.
We just keep track of current elements and their increasing/decreasing frequencies in a dict/table. At every iteration, if the frequency of the current element ($freq[nums[right]]$)
goes beyond k, we start shrinking the window from the left.
Code:
Python
Big O Analysis
-
Runtime
The runtime here is since we are visiting each element once.
-
Memory
We use a frequency table so the memory order is
— A