Maximum Number of Events That Can Be Attended - Complete Solution Guide
Maximum Number of Events That Can Be Attended is LeetCode problem 1353, 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 an array of events where events[i] = [startDay i , endDay i ] . Every event i starts at startDay i and ends at endDay i . You can attend an event i at any day d where startDay i <= d <= endDay i . You can only attend one event at any time d . Return the maximum number of events you can attend . Example 1: Input: events = [[1,2],[2,3],[3,4]] Output: 3 Explanation: You can attend all the three events. One way to attend them all is as shown. Attend the first event on day 1. Attend the
Detailed Explanation
The problem asks us to find the maximum number of events we can attend given a list of events, where each event has a start and end day. We can only attend one event per day, and we can only attend an event on a day between its start and end day (inclusive). The goal is to choose a schedule that maximizes the number of events attended.
Solution Approach
The solution uses a greedy approach combined with a min-heap. We first sort the events by their start day. Then, we iterate through each day, adding the end days of events that start on that day to the min-heap. We remove expired events from the min-heap (events whose end day is less than the current day). Finally, if the min-heap is not empty, we attend the event that ends earliest (the top of the min-heap) and increment the attended event count.
Step-by-Step Algorithm
- Step 1: Sort the events by their start day in ascending order.
- Step 2: Initialize a min-heap (priority queue) to store the end days of available events.
- Step 3: Initialize a counter 'attended_count' to 0.
- Step 4: Initialize an event index 'event_idx' to 0.
- Step 5: Iterate through each day 'd' from 1 up to the maximum possible end day (100000, as per the constraints).
- Step 6: While there are events starting on day 'd', add their end days to the min-heap.
- Step 7: Remove events from the min-heap that have already ended (end day < d).
- Step 8: If the min-heap is not empty, it means there's at least one event available. Attend the event that ends earliest (pop the minimum end day from the min-heap) and increment 'attended_count'.
- Step 9: After iterating through all days, return the 'attended_count'.
Key Insights
- Insight 1: Sorting the events by start day allows us to process events in chronological order, considering only events that are available on a given day.
- Insight 2: Using a min-heap (priority queue) to store the end days of available events allows us to efficiently choose the event that ends earliest, maximizing the number of days available to attend future events.
- Insight 3: Removing expired events from the heap on each day ensures we only consider events that are still valid for attendance, preventing us from wasting days on events that have already ended.
Complexity Analysis
Time Complexity: O(n log n)
Space Complexity: O(n)
Topics
This problem involves: Array, Greedy, Sorting, Heap (Priority Queue).
Companies
Asked at: Instacart, PayPal, Snowflake, Visa.