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.
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.
Input: events= [[1,2],[2,3],[3,4],[1,2]] Output: 4
1 <= events.length <= 105
events[i].length == 2
1 <= startDayi <= endDayi <= 105
use std::collections::BinaryHeap;
impl Solution {
pub fn max_events(events: Vec<Vec<i32>>) -> i32 {
let mut events = events;
let mut heap = BinaryHeap::new();
let first_day = events.iter().map(|e| e[0]).min().unwrap();
let final_day = events.iter().map(|e| e[1]).max().unwrap();
let mut i = 0;
let mut ret = 0;
events.sort_unstable();
for day in first_day..=final_day {
while i < events.len() && events[i][0] <= day {
heap.push(-events[i][1]);
i += 1;
}
while let Some(end_day) = heap.pop() {
if -end_day >= day {
ret += 1;
break;
}
}
}
ret
}
}