-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert-interval.rs
40 lines (35 loc) · 972 Bytes
/
insert-interval.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
struct Solution;
impl Solution {
pub fn insert(intervals: Vec<Vec<i32>>, new_interval: Vec<i32>) -> Vec<Vec<i32>> {
let mut intervals = intervals;
intervals.push(new_interval);
intervals.sort_unstable_by(|i, j| i[0].cmp(&j[0]));
let mut ans: Vec<Vec<i32>> = vec![];
for i in intervals.into_iter() {
if ans.is_empty() || i[0] > ans.last().unwrap()[1] {
ans.push(i);
} else if i[1] > ans.last().unwrap()[1] {
let last = ans.last_mut().unwrap();
*last = vec![last[0], i[1]];
}
}
ans
}
}
fn main() {
println!(
"{:?}",
Solution::insert(vec![vec![1, 3], vec![6, 9]], vec![2, 5])
);
}
#[cfg(test)]
mod tests {
use crate::Solution;
#[test]
fn test() {
assert_eq!(
Solution::insert(vec![vec![1, 3], vec![6, 9]], vec![2, 5]),
[[1, 5], [6, 9]]
)
}
}