1353. Maximum Number of Events That Can Be Attended LeetCode Solution
In this guide, you will get 1353. Maximum Number of Events That Can Be Attended LeetCode Solution with the best time and space complexity. The solution to Maximum Number of Events That Can Be Attended problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Number of Events That Can Be Attended solution in C++
- Maximum Number of Events That Can Be Attended solution in Java
- Maximum Number of Events That Can Be Attended solution in Python
- Additional Resources
Problem Statement of Maximum Number of Events That Can Be Attended
You are given an array of events where events[i] = [startDayi, endDayi]. Every event i starts at startDayi and ends at endDayi.
You can attend an event i at any day d where startTimei <= d <= endTimei. 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 second event on day 2.
Attend the third event on day 3.
Example 2:
Input: events= [[1,2],[2,3],[3,4],[1,2]]
Output: 4
Constraints:
1 <= events.length <= 105
events[i].length == 2
1 <= startDayi <= endDayi <= 105
Complexity Analysis
- Time Complexity: O(n\log n)
- Space Complexity: O(n)
1353. Maximum Number of Events That Can Be Attended LeetCode Solution in C++
class Solution {
public:
int maxEvents(vector<vector<int>>& events) {
int ans = 0;
int d = 0; // the current day
int i = 0; // events' index
priority_queue<int, vector<int>, greater<>> minHeap;
ranges::sort(events);
while (!minHeap.empty() || i < events.size()) {
// If no events are available to attend today, let time flies to the next
// available event.
if (minHeap.empty())
d = events[i][0];
// All the events starting from today are newly available.
while (i < events.size() && events[i][0] == d)
minHeap.push(events[i++][1]);
// Greedily attend the event that'll end the earliest since it has higher
// chance can't be attended in the future.
minHeap.pop();
++ans;
++d;
// Pop the events that can't be attended.
while (!minHeap.empty() && minHeap.top() < d)
minHeap.pop();
}
return ans;
}
};
/* code provided by PROGIEZ */
1353. Maximum Number of Events That Can Be Attended LeetCode Solution in Java
class Solution {
public int maxEvents(int[][] events) {
int ans = 0;
int d = 0; // the current day
int i = 0; // events' index
Queue<Integer> minHeap = new PriorityQueue<>();
Arrays.sort(events, (a, b) -> Integer.compare(a[0], b[0]));
while (!minHeap.isEmpty() || i < events.length) {
// If no events are available to attend today, let time flies to the next
// available event.
if (minHeap.isEmpty())
d = events[i][0];
// All the events starting from today are newly available.
while (i < events.length && events[i][0] == d)
minHeap.offer(events[i++][1]);
// Greedily attend the event that'll end the earliest since it has higher
// chance can't be attended in the future.
minHeap.poll();
++ans;
++d;
// Pop the events that can't be attended.
while (!minHeap.isEmpty() && minHeap.peek() < d)
minHeap.poll();
}
return ans;
}
}
// code provided by PROGIEZ
1353. Maximum Number of Events That Can Be Attended LeetCode Solution in Python
class Solution:
def maxEvents(self, events: list[list[int]]) -> int:
ans = 0
minHeap = []
i = 0 # events' index
events.sort(key=lambda x: x[0])
while minHeap or i < len(events):
# If no events are available to attend today, let time flies to the next
# available event.
if not minHeap:
d = events[i][0]
# All the events starting from today are newly available.
while i < len(events) and events[i][0] == d:
heapq.heappush(minHeap, events[i][1])
i += 1
# Greedily attend the event that'll end the earliest since it has higher
# chance can't be attended in the future.
heapq.heappop(minHeap)
ans += 1
d += 1
# Pop the events that can't be attended.
while minHeap and minHeap[0] < d:
heapq.heappop(minHeap)
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.