Leetcode 1353: Maximum Number of Events That Can Be Attended

grid47
grid47
Exploring patterns and algorithms
Jun 24, 2024 6 min read

You are given a list of events where each event is represented by a pair [startDay, endDay]. Your task is to find the maximum number of events you can attend, ensuring that you attend only one event per day.
Problem
Approach
Steps
Complexity
Input: The input consists of a list of events, where each event is represented by a pair of integers indicating the start and end day of the event.
Example: For example, [[1, 2], [2, 3], [3, 4]] where each pair represents an event that lasts from start to end day.
Constraints:
• 1 <= events.length <= 10^5
• events[i].length == 2
• 1 <= startDay[i] <= endDay[i] <= 10^5
Output: The output should be a single integer representing the maximum number of events you can attend.
Example: For the input [[1, 2], [2, 3], [3, 4]], the output should be 3.
Constraints:
• The result will always be a non-negative integer.
Goal: The goal is to maximize the number of events that can be attended by scheduling them on non-overlapping days.
Steps:
• 1. Sort the events based on their start day. If two events start on the same day, sort by the end day.
• 2. Use a priority queue (min-heap) to keep track of the end days of events that are currently active.
• 3. Iterate through the days and for each day, add any events that start on that day and remove events that have ended.
• 4. Track the maximum number of events that can be attended at any point in time.
Goal: The solution should handle large inputs efficiently, and the result should fit within a 32-bit integer.
Steps:
• 1 <= events.length <= 10^5
• 1 <= startDay[i] <= endDay[i] <= 10^5
Assumptions:
• Events do not overlap by default, but they may share common days.
Input: Example 1: [[1, 2], [2, 3], [3, 4]]
Explanation: In this case, you can attend all three events by selecting one event per day.

Input: Example 2: [[1, 2], [2, 3], [3, 4], [1, 2]]
Explanation: In this case, you can attend all four events by choosing the best days.

Link to LeetCode Lab


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