Skip to content

Commit

Permalink
create 0056-merge-intervals.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
rmrt1n committed Aug 20, 2023
1 parent 71b7a67 commit c53ee3c
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions rust/0056-merge-intervals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
impl Solution {
pub fn merge(mut intervals: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
intervals.sort_by(|a, b| a[0].cmp(&b[0]));
let acc = vec![intervals.first().unwrap().clone()];
intervals.into_iter().skip(1).fold(acc, |mut acc, e| {
let last = acc.last().unwrap();
if e[0] <= last[1] {
acc.last_mut().unwrap()[1] = last[1].max(e[1]);
} else {
acc.push(e)
}
acc
})
}
}

0 comments on commit c53ee3c

Please sign in to comment.