Skip to content

Commit a57acd4

Browse files
authored
Create frequency-of-the-most-frequent-element.cpp
1 parent bc260c3 commit a57acd4

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Time: O(nlogn)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
int maxFrequency(vector<int>& nums, int k) {
7+
sort(begin(nums), end(nums));
8+
int left = 0, right = 0;
9+
for (int64_t total = k ; right < size(nums); ++right) {
10+
total += nums[right];
11+
if (total < int64_t(nums[right]) * (right - left + 1)) {
12+
total -= nums[left++];
13+
}
14+
}
15+
return (right - 1) - left + 1;
16+
}
17+
};

0 commit comments

Comments
 (0)