Python clear solution | Leetcode 2054 - Two Best Non-Overlapping Events
Clearly, maximum score will be achieved with two non-overlapping segments.\nAs a result, we can see the link of this problem to Leetcode 2054.\n\n    def maximi
Clearly, maximum score will be achieved with `two non-overlapping segments`.\nAs a result, we can see the link of this problem to Leetcode 2054.\n\n    def maximizeWin(self, ps: List[int], k: int) -> int:\n        if max(ps)-min(ps)<=k*2:\n            return len(ps)\n\n        scores, q, cur, ans = [], [], 0, 0\n\t\t# build the list of (start, end, prize-numbers)\n        for i, n in enumerate(ps):\n            if not scores or scores[-1][0]!=n:\n                idx = bisect.bisect_left(ps, n+k+1)\n                scores.append([n, ps[idx-1], idx-i])\n\t\n\t\t# calculate best score of two Non-overlapping segments\n        for i, j, v in scores:\n            while(q and q[0][0]<i):\n                cur = max(cur, heappop(q)[1])\n            ans = max(ans, cur+v)\n            heappush(q, (j, v))\n        return ans
[]