Sweep Line Algorithm
The picture depicts the idea of the sweep line algo:\n\nThe picture is for reference only.\n\nHere, instead of +1/-1 I use +frequency/-frequency.\nAt each event
The picture depicts the idea of the sweep line algo:\n![image](https://assets.leetcode.com/users/images/187e22cc-e911-4e9a-86d4-c1a5aba5c97e_1731282268.8791971.png)\nThe picture is for reference only.\n\nHere, instead of `+1/-1` I use `+frequency/-frequency`.\nAt each `event` I pick max `overlaps + freq[e]`.\nOverlaps that you can pick is limited by `numOps`. Thus `min(overlaps, numOps)`.\nTaking into account that overlaps already includes `freq[e]`, we need to substract it to get `only overlaps`.\n\n```\nclass Solution:\n    def maxFrequency(self, nums: List[int], k: int, numOperations: int) -> int:\n        freq = Counter(nums)\n        line = []\n        for num,f in freq.items():\n            line.append((num - k, f))\n            line.append((num, 0))\n            line.append((num + k + 1, -f))\n        line.sort()\n        max_freq = 0\n        overlaps = 0\n        for e,d in line:\n            overlaps += d\n            max_freq = max(max_freq, min(numOperations, max(0, overlaps - freq[e])) + freq[e])\n        return max_freq\n```
['Python', 'Python3']