EASY TO MEDIUM C++ SOLUTION SOLVED ||  2054. Two Best Non-Overlapping Events
Intuition\n\nThe goal is to find the maximum value of two non-overlapping events from a list of events, where each event has a start time, end time, and value. 
# Intuition\n\nThe goal is to find the maximum value of two non-overlapping events from a list of events, where each event has a start time, end time, and value. The optimal approach involves sorting the events by their start times and leveraging a priority queue to track the maximum value of events that end before the current event\'s start.\n\n# Approach\n\n1. Sort Events: Sort the events based on their start times to process them in chronological order.\n2. Use a Priority Queue: Use a priority queue (min-heap) to store events as {end time, value}. This allows efficient removal of events that end before the current event\'s start.\n3. Iterate Through Events: For each event: Remove events from the priority queue whose end time is less than the current event\'s start time. While doing so, update maxVal with the maximum value among removed events.\nCalculate the potential maximum value as maxVal + current event value.\nPush the current event into the priority queue.\n4. Update the Answer: Track the maximum value (ans) obtained across all iterations.\n5. Return the Result: Return ans as the maximum value of two non-overlapping events.\n\n# Complexity\n- Time complexity: O(nlogn), Sorting the events takes O(nlogn). Each event is pushed and popped from the priority queue at most once, costing O(logn) per operation.\n\n- Space complexity: O(n), The priority queue can store up to n events in the worst case.\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int maxTwoEvents(vector<vector<int>>& events) {\n        sort(events.begin(), events.end()); // Sort events by start time\n        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;\n        int maxVal = 0, ans = 0;\n\n        for (auto& event : events) {\n            int start = event[0], end = event[1], value = event[2];\n\n            // Remove events that end before the current event\'s start time\n            while (!pq.empty() && pq.top().first < start) {\n                maxVal = max(maxVal, pq.top().second);\n                pq.pop();\n            }\n\n            // Update the maximum sum of values\n            ans = max(ans, maxVal + value);\n\n            // Add the current event to the priority queue\n            pq.push({end, value});\n        }\n\n        return ans;\n    }\n};\n```
['Array', 'Sorting', 'Heap (Priority Queue)', 'C++']