Simple Solution with Priority Queue | ✅Beats 100% | C++ | Java | Python | JavaScript
\n\n# \u2B06\uFE0FUpvote if it helps \u2B06\uFE0F  \n\n---\n\n## Connect with me on Linkedin Bijoy Sing.  \n\n---\n\n\n## Follow me also on Codeforces: Bijoy Si
\n\n# \u2B06\uFE0FUpvote if it helps \u2B06\uFE0F  \n\n---\n\n## Connect with me on Linkedin [Bijoy Sing](https://www.linkedin.com/in/bijoy-sing-236a5a1b2/).  \n\n---\n\n\n## Follow me also on Codeforces: [Bijoy Sing](https://codeforces.com/profile/BijoySingh7)  \n\n---\n\n\n###### *Solution in C++, Python, Java, and JavaScript*  \n\n```cpp []\nclass Solution {\npublic:\n    int maxTwoEvents(vector<vector<int>>& events) {\n        sort(events.begin(), events.end());\n        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;\n        int maxVal = 0, ans = 0;\n\n        for (const auto& event : events) {\n            int start = event[0], end = event[1], value = event[2];\n\n            while (!pq.empty() && pq.top().first < start) {\n                maxVal = max(maxVal, pq.top().second);\n                pq.pop();\n            }\n\n            ans = max(ans, maxVal + value);\n            pq.push({end, value});\n        }\n\n        return ans;\n    }\n};\n```\n\n```python []\nimport heapq\n\nclass Solution:\n    def maxTwoEvents(self, events):\n        events.sort()\n        pq = []\n        max_val = 0\n        ans = 0\n\n        for start, end, value in events:\n            while pq and pq[0][0] < start:\n                max_val = max(max_val, heapq.heappop(pq)[1])\n            ans = max(ans, max_val + value)\n            heapq.heappush(pq, (end, value))\n\n        return ans\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        PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> Integer.compare(a[0], b[0]));\n        int maxVal = 0, ans = 0;\n\n        for (int[] event : events) {\n            int start = event[0], end = event[1], value = event[2];\n\n            while (!pq.isEmpty() && pq.peek()[0] < start) {\n                maxVal = Math.max(maxVal, pq.poll()[1]);\n            }\n\n            ans = Math.max(ans, maxVal + value);\n            pq.add(new int[]{end, value});\n        }\n\n        return ans;\n    }\n}\n```\n\n```javascript []\n/**\n * @param {number[][]} events\n * @return {number}\n */\nvar maxTwoEvents = function(events) {\n    events.sort((a, b) => a[0] - b[0]);\n    const n = events.length;\n    const prefixMax = new Array(n).fill(0);\n    \n    prefixMax[n - 1] = events[n - 1][2];\n    for (let i = n - 2; i >= 0; i--) {\n        prefixMax[i] = Math.max(prefixMax[i + 1], events[i][2]);\n    }\n    \n    let maxResult = 0;\n    \n    for (let i = 0; i < n; i++) {\n        const currValue = events[i][2];\n        const currEnd = events[i][1];\n        \n        let left = i + 1, right = n - 1;\n        let maxNextValue = 0;\n        \n        while (left <= right) {\n            const mid = Math.floor((left + right) / 2);\n            if (events[mid][0] > currEnd) {\n                maxNextValue = Math.max(maxNextValue, prefixMax[mid]);\n                right = mid - 1;\n            } else {\n                left = mid + 1;\n            }\n        }\n        \n        maxResult = Math.max(maxResult, currValue + maxNextValue);\n    }\n    \n    return maxResult;\n};\n```\n\n# Intuition  \nTo solve the problem, we need to find two non-overlapping events that maximize the total value. Sorting the events by start time helps us efficiently determine which events can be paired.  \n\n# Approach  \n1. Sort the events based on their start time.  \n2. Use a priority queue to keep track of events that end before the current event starts.  \n3. Track the maximum value of completed events (`maxVal`) to calculate the best combination with the current event.  \n4. Update the global maximum (`ans`) and continue processing.  \n\n# Complexity  \n- Time complexity:  \n  $$O(n \\log n)$$ (due to sorting and priority queue operations).  \n\n- Space complexity:  \n  $$O(n)$$ (to store events in the priority queue).  \n\n### *If you have any questions or need further clarification, feel free to drop a comment! \uD83D\uDE0A*
['Array', 'Binary Search', 'Dynamic Programming', 'Sorting', 'Heap (Priority Queue)', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']