Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f9f2f1a

Browse files
author
guangsheng.li01
committed
Solved a0646
1 parent 6b4b3ab commit f9f2f1a

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

‎src/a0646_maximum_length_of_pair_chain.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,18 @@ pub struct Solution {}
77
// solution impl starts here
88

99
impl Solution {
10-
fn add(a: i32, b: i32) -> i32 {
11-
a + b
10+
pub fn find_longest_chain(pairs: Vec<Vec<i32>>) -> i32 {
11+
let mut pairs = pairs;
12+
pairs.sort_by_key(|p| p[1]);
13+
let mut t = pairs[0][1];
14+
let mut count = 1;
15+
for p in pairs {
16+
if p[0] > t {
17+
count += 1;
18+
t = p[1];
19+
}
20+
}
21+
count
1222
}
1323
}
1424

@@ -22,7 +32,27 @@ mod tests {
2232

2333
#[test]
2434
fn test_case0() {
25-
assert_eq!(5, Solution::add(2, 3));
35+
assert_eq!(
36+
Solution::find_longest_chain(vec![vec![1, 2], vec![2, 3], vec![3, 4]]),
37+
2
38+
);
39+
}
40+
41+
#[test]
42+
fn test_case1() {
43+
assert_eq!(
44+
Solution::find_longest_chain(vec![
45+
vec![-10, -8],
46+
vec![8, 9],
47+
vec![-5, 0],
48+
vec![6, 10],
49+
vec![-6, -4],
50+
vec![1, 7],
51+
vec![9, 10],
52+
vec![-4, 7]
53+
]),
54+
4
55+
);
2656
}
2757
}
2858

‎src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ mod a0404_sum_of_left_leaves;
2323
mod a0405_convert_a_number_to_hexadecimal;
2424
mod a0617_merge_two_binary_trees;
2525
mod a0643_maximum_average_subarray_i;
26+
mod a0646_maximum_length_of_pair_chain;
2627
mod a0867_transpose_matrix;
2728
mod a0929_unique_email_addresses;
2829
mod a0931_minimum_falling_path_sum;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /