Skip to content

Commit 867a597

Browse files
authored
Create length-of-longest-subarray-with-at-most-k-frequency.cpp
1 parent ebde25f commit 867a597

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(n)
2+
// Space: o(n)
3+
4+
// freq table, two pointers, sliding window
5+
class Solution {
6+
public:
7+
int maxSubarrayLength(vector<int>& nums, int k) {
8+
unordered_map<int, int> cnt;
9+
int result = 0;
10+
for (int right = 0, left = 0; right < size(nums); ++right) {
11+
++cnt[nums[right]];
12+
while (!(cnt[nums[right]] <= k)) {
13+
--cnt[nums[left++]];
14+
}
15+
result = max(result, right - left + 1);
16+
}
17+
return result;
18+
}
19+
};

0 commit comments

Comments
 (0)