## 1353. Maximum Number of Events That Can Be Attended

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`. Notice that 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
```

Example 3:

```Input: events = [[1,4],[4,4],[2,2],[3,4],[1,1]]
Output: 4
```

Example 4:

```Input: events = [[1,100000]]
Output: 1
```

Example 5:

```Input: events = [[1,1],[1,2],[1,3],[1,4],[1,5],[1,6],[1,7]]
Output: 7
```

Constraints:

• `1 <= events.length <= 105`
• `events[i].length == 2`
• `1 <= startDayi <= endDayi <= 105`

## Rust Solution

``````struct Solution;
use std::cmp::Reverse;
use std::collections::BinaryHeap;

impl Solution {
fn max_events(mut events: Vec<Vec<i32>>) -> i32 {
events.sort_by_key(|e| e[0]);
let mut queue: BinaryHeap<Reverse<i32>> = BinaryHeap::new();
let max = events.iter().map(|e| e[1]).max().unwrap();
let mut it = events.into_iter().peekable();
let mut res = 0;
for i in 1..=max {
while let Some(&front) = queue.peek() {
if front.0 < i {
queue.pop();
} else {
break;
}
}
while let Some(front) = it.peek() {
if front[0] == i {
queue.push(Reverse(front[1]));
it.next();
} else {
break;
}
}
if queue.pop().is_some() {
res += 1;
}
}
res
}
}

#[test]
fn test() {
let events = vec_vec_i32![[1, 2], [2, 3], [3, 4]];
let res = 3;
assert_eq!(Solution::max_events(events), res);
let events = vec_vec_i32![[1, 2], [2, 3], [3, 4], [1, 2]];
let res = 4;
assert_eq!(Solution::max_events(events), res);
let events = vec_vec_i32![[1, 4], [4, 4], [2, 2], [3, 4], [1, 1]];
let res = 4;
assert_eq!(Solution::max_events(events), res);
let events = vec_vec_i32![[1, 100000]];
let res = 1;
assert_eq!(Solution::max_events(events), res);
let events = vec_vec_i32![[1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7]];
let res = 7;
assert_eq!(Solution::max_events(events), res);
}
``````

Having problems with this solution? Click here to submit an issue on github.