Binary Search Approach ✅✅
Intuition\nTo solve this problem, we need to select at most two non-overlapping events to maximize the total value. Sorting the events by start time simplifies 
# Intuition\nTo solve this problem, we need to select at most two non-overlapping events to maximize the total value. Sorting the events by start time simplifies the problem, allowing efficient checks for overlaps and the use of binary search to find valid second events.\n\n# Approach\n1. **Sort Events**: First, sort the events by their start times.\n2. **Precompute Maximum Values**: Create a `maxValue` array where `maxValue[i]` stores the maximum value from event `i` to the end. This allows for quick access to the best choice for the second event.\n3. **Binary Search for Non-Overlapping Events**: For each event, use binary search to find the next non-overlapping event. Combine the values of the current event and the best non-overlapping event to calculate the maximum value.\n4. **Track Maximum Result**: Update the result iteratively for the best possible outcome.\n\n# Complexity\n- Time complexity:  \n  Sorting takes $$(O(n \\log n)$$, precomputing the maximum values takes $$(O(n)$$, and binary search for each event takes $$(O(n \\log n)$$.  \n  Overall: $$(O(n \\log n)$$.\n  \n- Space complexity:  \n  Additional space is used for the `maxValue` array, which is $$O(n)$$\n\n# Code\n``` cpp []\nclass Solution {\npublic:\n    int maxTwoEvents(vector<vector<int>>& events) {\n        sort(events.begin(), events.end());\n        int n = events.size();\n\n        vector<int> maxValue(n, 0);\n        maxValue[n - 1] = events[n - 1][2];\n        for (int i = n - 2; i >= 0; --i) {\n            maxValue[i] = max(maxValue[i + 1], events[i][2]);\n        }\n\n        int result = 0;\n\n        for (int i = 0; i < n; ++i) {\n            int currValue = events[i][2];\n            result = max(result, currValue);\n\n            int lo = i + 1, hi = n - 1, nextEvent = -1;\n            while (lo <= hi) {\n                int mid = lo + (hi - lo) / 2;\n                if (events[mid][0] > events[i][1]) {\n                    nextEvent = mid;\n                    hi = mid - 1;\n                } else {\n                    lo = mid + 1;\n                }\n            }\n\n            if (nextEvent != -1) {\n                result = max(result, currValue + maxValue[nextEvent]);\n            }\n        }\n\n        return result;\n    }\n};\n```\n``` Java []\nimport java.util.*;\n\nclass Solution {\n    public int maxTwoEvents(int[][] events) {\n        Arrays.sort(events, (a, b) -> Integer.compare(a[0], b[0]));\n        int n = events.length;\n\n        int[] maxValue = new int[n];\n        maxValue[n - 1] = events[n - 1][2];\n        for (int i = n - 2; i >= 0; --i) {\n            maxValue[i] = Math.max(maxValue[i + 1], events[i][2]);\n        }\n\n        int result = 0;\n\n        for (int i = 0; i < n; ++i) {\n            int currValue = events[i][2];\n            result = Math.max(result, currValue);\n\n            int lo = i + 1, hi = n - 1, nextEvent = -1;\n            while (lo <= hi) {\n                int mid = lo + (hi - lo) / 2;\n                if (events[mid][0] > events[i][1]) {\n                    nextEvent = mid;\n                    hi = mid - 1;\n                } else {\n                    lo = mid + 1;\n                }\n            }\n\n            if (nextEvent != -1) {\n                result = Math.max(result, currValue + maxValue[nextEvent]);\n            }\n        }\n\n        return result;\n    }\n}\n\n```\n``` JavaScript []\nvar maxTwoEvents = function(events) {\n    events.sort((a, b) => a[0] - b[0]);\n    const n = events.length;\n\n    const maxValue = new Array(n).fill(0);\n    maxValue[n - 1] = events[n - 1][2];\n    for (let i = n - 2; i >= 0; --i) {\n        maxValue[i] = Math.max(maxValue[i + 1], events[i][2]);\n    }\n\n    let result = 0;\n\n    for (let i = 0; i < n; ++i) {\n        const currValue = events[i][2];\n        result = Math.max(result, currValue);\n\n        let lo = i + 1, hi = n - 1, nextEvent = -1;\n        while (lo <= hi) {\n            const mid = lo + Math.floor((hi - lo) / 2);\n            if (events[mid][0] > events[i][1]) {\n                nextEvent = mid;\n                hi = mid - 1;\n            } else {\n                lo = mid + 1;\n            }\n        }\n\n        if (nextEvent !== -1) {\n            result = Math.max(result, currValue + maxValue[nextEvent]);\n        }\n    }\n\n    return result;\n};\n\n```\n``` Python []\nclass Solution:\n    def maxTwoEvents(self, events: List[List[int]]) -> int:\n        events.sort()\n        n = len(events)\n\n        maxValue = [0] * n\n        maxValue[-1] = events[-1][2]\n        for i in range(n - 2, -1, -1):\n            maxValue[i] = max(maxValue[i + 1], events[i][2])\n\n        result = 0\n\n        for i in range(n):\n            currValue = events[i][2]\n            result = max(result, currValue)\n\n            lo, hi, nextEvent = i + 1, n - 1, -1\n            while lo <= hi:\n                mid = lo + (hi - lo) // 2\n                if events[mid][0] > events[i][1]:\n                    nextEvent = mid\n                    hi = mid - 1\n                else:\n                    lo = mid + 1\n\n            if nextEvent != -1:\n                result = max(result, currValue + maxValue[nextEvent])\n\n        return result\n\n```
['Binary Search', 'Python', 'C++', 'Java', 'Python3', 'JavaScript', 'Python ML']