1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Solution { public: int maxFrequency(vector<int>& nums, int k) { const int n = nums.size(); sort(nums.begin(), nums.end()); int l = 0, ans = 0; long sum = 0; for (int r = 0; r < n; ++r) { sum += nums[r]; while (l < r && sum + k < static_cast<long>(nums[r]) * (r - l + 1)) sum -= nums[l++]; ans = max(ans, r - l + 1); } return ans; } };
|