Problem Description
Given an array of events where each event is represented as [startTime, endTime, value], choose at most two non-overlapping events (i.e. the second event must start strictly after the first event ends) so that the sum of their values is maximized. Return the maximum possible sum.
Key Insights
- Sort events by start time. This allows for binary search to quickly find the next non-overlapping event.
- Precompute a suffix maximum array where each position stores the maximum event value among all events starting from that index.
- For each event, via binary search, determine the earliest event that can be attended after the current one, and combine the current event’s value with the best value from the remainder of the list.
- Consider the case where taking a single event is optimal.
Space and Time Complexity
Time Complexity: O(n log n) – O(n log n) for sorting and binary search over each event. Space Complexity: O(n) – for the auxiliary arrays used (start times and suffix maximum values).
Solution
The algorithm sorts the events by their start time. Then, it builds an array that holds the maximum event value from any index to the end (suffix max). For each event, a binary search is used on the sorted start times to find the next event that can be attended (i.e., the event whose start time is at least the current event's end time plus one). The value of the current event is added to the precomputed best value from that next event, and the maximum sum is updated accordingly. This approach ensures that the best combination of two non-overlapping events is found while keeping complexity under control.