Two Best Non-Overlapping Events - Complete Solution Guide
Two Best Non-Overlapping Events is LeetCode problem 2054, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given a 0-indexed 2D integer array of events where events[i] = [startTime i , endTime i , value i ] . The i th event starts at startTime i and ends at endTime i , and if you attend this event, you will receive a value of value i . You can choose at most two non-overlapping events to attend such that the sum of their values is maximized . Return this maximum sum. Note that the start time and end time is inclusive : that is, you cannot attend two events where one of them starts and the oth
Detailed Explanation
The problem asks us to find the maximum sum of values that can be obtained by attending at most two non-overlapping events. Each event has a start time, an end time, and a value. Two events are considered non-overlapping if the end time of the first event is strictly less than the start time of the second event (end_time1 < start_time2). The input is a 2D array where each row represents an event with [startTime, endTime, value]. The output should be the maximum sum of values from at most two non-overlapping events.
Solution Approach
The solution uses a combination of sorting and a min-heap to efficiently find the maximum sum of values from two non-overlapping events. First, the events are sorted by their start times. Then, we iterate through the sorted events. For each event, we use a min-heap to keep track of the events that have already started but haven't ended. We remove events from the min-heap whose end times are less than the start time of the current event. While removing elements from the min heap, we keep track of the maximum value seen so far in `max_val_prefix`. The maximum sum is then updated as the maximum of the current maximum sum and the sum of the current event's value and the `max_val_prefix`. Finally, the current event is added to the min-heap.
Step-by-Step Algorithm
- Step 1: Sort the events by their start times in ascending order.
- Step 2: Initialize `max_sum` and `max_val_prefix` to 0. Also, initialize an empty min-heap.
- Step 3: Iterate through the sorted events.
- Step 4: For each event, remove all events from the min-heap that have ended before the current event starts. Update `max_val_prefix` while removing elements.
- Step 5: Update `max_sum` to be the maximum of its current value and `max_val_prefix + value` of current event.
- Step 6: Add the current event's end time and value to the min-heap.
- Step 7: Return `max_sum`.
Key Insights
- Insight 1: Sorting events by their start times allows us to process them in a sequential manner and efficiently determine potential non-overlapping event pairs.
- Insight 2: Using a min-heap (priority queue) helps in tracking the events that have already started but haven't ended yet, ordered by their end times. This allows us to efficiently find the maximum value of events that have already ended when considering a new event.
- Insight 3: Maintaining a `max_val_prefix` variable keeps track of the maximum value among all the events that have already ended. This helps in quickly calculating the potential sum of the current event with a non-overlapping event that has already finished.
Complexity Analysis
Time Complexity: O(n log n)
Space Complexity: O(n)
Topics
This problem involves: Array, Binary Search, Dynamic Programming, Sorting, Heap (Priority Queue).
Companies
Asked at: razorpay.