(1) Optimized Greedy in Array, (2) Max HeapQueue with Explanation
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nGreedy in Array.\n\n# Complexity\n- Time complexity: O(n * log n)\n Add y
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nGreedy in Array.\n\n# Complexity\n- Time complexity: O(n * log n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\nclass Solution:\n    def maxTwoEvents(self, events: List[List[int]]) -> int:\n        event = []       # store (time, load/unload, value)\n\n        for start, end, value in events:\n            event += [(start, 1, value)]\n            event += [(end + 1, -1, value)]\n\n        # sort based on the time increasingly\n        event = sorted(event)\n\n        res = 0            # the maximum aggregation we can build so far\n        max_value = 0      # the maximum value seen so far \n\n        for time, load, value in event:\n            # see a new job, update the aggregation to \n            if load == 1:\n                res = max(res, max_value + value)\n                \n            # otherwise, only update max_val seen so far\n            else:\n                max_value = max(max_value, value)\n\n        return res\n```\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nMax HeapQueue.\n\n# Complexity\n- Time complexity: O(n * log n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\nclass Solution:\n    def maxTwoEvents(self, events: List[List[int]]) -> int:\n        maxhpq = [(0, float(\'inf\'))]       # max heap storing (value, starting time)\n\n        for start, _, value in events:\n            heappush(maxhpq, (-value, start))\n\n        # 1. sort based on the ending time increasingly\n        events = sorted(events, key=lambda x: x[1])\n\n        res = 0            # the maximum aggregation we can build so far\n        max_value = 0      # the maximum value seen so far \n\n        # 2. iterate over sorted evnets \n        for _, end, value in events:\n            # update\n            max_value = max(max_value, value)\n\n            # clean up max heap if the current and is greater than max heap time\n            while maxhpq and end >= maxhpq[0][1]:\n                heappop(maxhpq)\n\n            # check the aggregation \n            res = max(res, max_value - maxhpq[0][0])\n\n        return res\n```
['Array', 'Binary Search', 'Dynamic Programming', 'Greedy', 'Sorting', 'Heap (Priority Queue)', 'Python', 'Python3']