-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3347.cpp
More file actions
27 lines (27 loc) · 801 Bytes
/
3347.cpp
File metadata and controls
27 lines (27 loc) · 801 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) {
unordered_map<int, int> mp;
set<int> st;
for (auto& num : nums) {
mp[num]++;
st.insert(num - k);
st.insert(num);
st.insert(num + k);
}
sort(nums.begin(), nums.end());
int n = nums.size();
int left = 0;
int right = 0;
int res = 0;
for (int num : st) {
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;
}
};