-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3346.cpp
More file actions
28 lines (27 loc) · 847 Bytes
/
3346.cpp
File metadata and controls
28 lines (27 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
int maxFrequency(vector<int>& nums, int k, int numOperations) {
int maxNum = 0;
int minNum = INT_MAX;
unordered_map<int, int> mp;
for (auto& num : nums) {
mp[num]++;
maxNum = max(num, maxNum);
minNum = min(num, minNum);
}
sort(nums.begin(), nums.end());
int n = nums.size();
int left = 0;
int right = 0;
int res = 0;
for (int num = minNum; num <= maxNum; ++num) {
while (left < n && nums[left] + k < num) left++;
while (right < n && nums[right] - k <= num) right++;
// [left, right)
int cnt = right - left;
int curr = mp[num];
res = max(res, curr + min(cnt - curr, numOperations));
}
return res;
}
};