Created
July 7, 2025 10:55
-
-
Save SuryaPratapK/77359fb7243e3ae185fc158cce604d77 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int maxEvents(vector<vector<int>>& events) { | |
sort(events.begin(),events.end()); | |
int n = events.size(); | |
int pos = 0; | |
int time = 1; | |
int attended = 0; | |
priority_queue<int,vector<int>,greater<int>> minheap; | |
while(pos<n or !minheap.empty()){ | |
//Time skip: to cover only timeline covered by atleast one event | |
if(minheap.empty()) | |
time = max(time,events[pos][0]); | |
//Add events starting at current pos | |
while(pos<n and events[pos][0]==time){ | |
minheap.push(events[pos][1]); | |
pos++; | |
} | |
//Remove all already ended (unattended) events | |
while(!minheap.empty() and minheap.top()<time) | |
minheap.pop(); | |
//Pop 1 event at current time | |
if(!minheap.empty()){ | |
minheap.pop(); | |
attended++; | |
} | |
time++; | |
} | |
return attended; | |
} | |
}; | |
/* | |
//JAVA | |
import java.util.*; | |
class Solution { | |
public int maxEvents(int[][] events) { | |
Arrays.sort(events, (a, b) -> a[0] - b[0]); | |
int n = events.length; | |
int pos = 0; | |
int time = 1; | |
int attended = 0; | |
PriorityQueue<Integer> minHeap = new PriorityQueue<>(); | |
while (pos < n || !minHeap.isEmpty()) { | |
// Time skip: to cover only timeline covered by at least one event | |
if (minHeap.isEmpty()) { | |
time = Math.max(time, events[pos][0]); | |
} | |
// Add events starting at current time | |
while (pos < n && events[pos][0] == time) { | |
minHeap.offer(events[pos][1]); | |
pos++; | |
} | |
// Remove all already ended (unattended) events | |
while (!minHeap.isEmpty() && minHeap.peek() < time) { | |
minHeap.poll(); | |
} | |
// Pop 1 event at current time | |
if (!minHeap.isEmpty()) { | |
minHeap.poll(); | |
attended++; | |
} | |
time++; | |
} | |
return attended; | |
} | |
} | |
#Python | |
import heapq | |
class Solution: | |
def maxEvents(self, events: List[List[int]]) -> int: | |
events.sort() | |
n = len(events) | |
pos = 0 | |
time = 1 | |
attended = 0 | |
min_heap = [] | |
while pos < n or min_heap: | |
# Time skip: to cover only timeline covered by at least one event | |
if not min_heap: | |
time = max(time, events[pos][0]) | |
# Add events starting at current time | |
while pos < n and events[pos][0] == time: | |
heapq.heappush(min_heap, events[pos][1]) | |
pos += 1 | |
# Remove all already ended (unattended) events | |
while min_heap and min_heap[0] < time: | |
heapq.heappop(min_heap) | |
# Pop 1 event at current time | |
if min_heap: | |
heapq.heappop(min_heap) | |
attended += 1 | |
time += 1 | |
return attended | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment