Leetcode 2054: Two Best Non-Overlapping Events

grid47
grid47
Exploring patterns and algorithms
Apr 15, 2024 9 min read

Find the maximum sum of values you can achieve by attending at most two non-overlapping events.
Problem
Approach
Steps
Complexity
Input: A list of events where each event is represented by its start time, end time, and value.
Example: Input: events = [[2, 4, 5], [5, 6, 3], [1, 3, 4]]
Constraints:
• 2 <= events.length <= 10^5
• events[i].length == 3
• 1 <= startTime_i <= endTime_i <= 10^9
• 1 <= value_i <= 10^6
Output: An integer representing the maximum value obtainable by attending at most two non-overlapping events.
Example: Output: 9 for the given input events.
Constraints:
Goal: Calculate the maximum value by attending up to two non-overlapping events.
Steps:
• Sort the events by their end times.
• Iterate over the events in reverse to find the maximum value obtainable from a single event.
• Use a binary search to find the next non-overlapping event for each event.
• Compute the maximum sum of values for one and two events.
Goal: The input size and properties of the events.
Steps:
• 2 <= events.length <= 10^5
• events[i].length == 3
• 1 <= startTime_i <= endTime_i <= 10^9
• 1 <= value_i <= 10^6
Assumptions:
• Events are provided in arbitrary order.
• Two events are non-overlapping if the second event's start time is strictly greater than the first event's end time.
Input: events = [[2, 4, 5], [5, 6, 3], [1, 3, 4]]
Explanation: Attend events 0 and 1 for a sum of 5 + 3 = 9.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus