-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeeting_rooms.rs
68 lines (57 loc) · 1.62 KB
/
meeting_rooms.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::cmp::Reverse;
use std::collections::BinaryHeap;
#[derive(Copy, Clone, PartialOrd, PartialEq, Eq, Ord, Hash)]
struct Meeting {
start: i32,
end: i32,
}
impl Meeting {
fn new(start: i32, end: i32) -> Self {
Self { start, end }
}
}
/// Given an array of meeting time `intervals` where
/// `intervals[i] = [starti, endi]`, determine if a person could attend all
/// meetings.
struct Solution;
impl Solution {
pub fn can_attend_meetings(intervals: Vec<Vec<i32>>) -> bool {
let mut min_heap = BinaryHeap::new();
for interval in intervals {
min_heap.push(Reverse(Meeting::new(interval[0], interval[1])));
}
let mut attending = None;
let mut result = true;
while !min_heap.is_empty() {
let next = min_heap.pop().unwrap().0;
if attending.is_none() {
attending = Some(next);
} else {
let current = attending.unwrap();
if next.start >= current.end {
attending = Some(next);
} else {
result = false;
break;
}
}
}
result
}
}
#[cfg(test)]
mod tests {
use super::Solution;
#[test]
fn example_1() {
let intervals = vec![vec![0, 30], vec![5, 10], vec![15, 20]];
let result = Solution::can_attend_meetings(intervals);
assert!(!result);
}
#[test]
fn example_2() {
let intervals = vec![vec![7, 10], vec![2, 4]];
let result = Solution::can_attend_meetings(intervals);
assert!(result);
}
}