作者:
sixB (6B)
2025-10-21 23:33:103346.
好感動
去年的我一整排wa
今年的我ac了><
當meeting room 在寫
看起來是可以不用開map比較快ㄛ
class Solution {
public:
int maxFrequency(vector<int>& nums, int k, int op) {
ranges::sort(nums);
unordered_map<int, int> cnt;
for(int i: nums) {
cnt[i]++;
}
map<int, int> mp;
for(auto [i, c] : cnt){
mp[i-k] += c;
mp[i];
mp[i+k+1] -= c;
}
int ma = 0, cur = 0, cntt = 0;
for(auto [i, c]: mp){
cur += c;
//cout << i << " " << c << " " << cur << " " << ma << '\n';
if(cur == ma){
if(cnt[i] > cntt){
cntt = cnt[i];
}
}
else if(cur > ma){
int t = min(cnt[i] + op, cur);
if(t > ma) {
ma = t;
cntt = cnt[i];
}
}
}
return ma;
}
};