Maximum Length of Pair Chain LeetCode solution in Rust

1
impl Solution {
2
pub fn find_longest_chain(mut pairs: Vec<Vec<i32>>) -> i32 {
3
// sort by second element of the pair
4
pairs.sort_by(|a, b| a[1].cmp(&b[1]));
5

6
// count the number of pairs that dont overlap
7
let mut count = 1;
8
let mut prev = pairs[0][1];
9
for i in 1..pairs.len() {
10
if pairs[i][0] > prev {
11
count += 1;
12
prev = pairs[i][1];
13
}
14
}
15

16
count
17
}
18
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0