Skip to content

Commit 25f98e1

Browse files
committed
+ problem 646
1 parent dd076d4 commit 25f98e1

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# 646. Maximum Length of Pair Chain
2+
You are given an array of `n` pairs `pairs` where <code>pairs[i] = [left<sub>i</sub>, right<sub>i</sub>]</code> and <code>left<sub>i</sub> < right<sub>i</sub></code>.
3+
4+
A pair `p2 = [c, d]` **follows** a pair `p1 = [a, b]` if `b < c`. A **chain** of pairs can be formed in this fashion.
5+
6+
Return *the length longest chain which can be formed*.
7+
8+
You do not need to use up all the given intervals. You can select pairs in any order.
9+
10+
#### Example 1:
11+
<pre>
12+
<strong>Input:</strong> pairs = [[1,2],[2,3],[3,4]]
13+
<strong>Output:</strong> 2
14+
<strong>Explanation:</strong> The longest chain is [1,2] -> [3,4].
15+
</pre>
16+
17+
#### Example 2:
18+
<pre>
19+
<strong>Input:</strong> pairs = [[1,2],[7,8],[4,5]]
20+
<strong>Output:</strong> 3
21+
<strong>Explanation:</strong> The longest chain is [1,2] -> [4,5] -> [7,8].
22+
</pre>
23+
24+
#### Constraints:
25+
* `n == pairs.length`
26+
* `1 <= n <= 1000`
27+
* <code>-1000 <= left<sub>i</sub> < right<sub>i</sub> <= 1000</code>
28+
29+
## Solutions (Rust)
30+
31+
### 1. Solution
32+
```Rust
33+
impl Solution {
34+
pub fn find_longest_chain(mut pairs: Vec<Vec<i32>>) -> i32 {
35+
pairs.sort_unstable_by_key(|p| (p[1], -p[0]));
36+
let mut right = pairs[0][1];
37+
let mut ret = 1;
38+
39+
for i in 1..pairs.len() {
40+
if pairs[i][0] > right {
41+
right = pairs[i][1];
42+
ret += 1;
43+
}
44+
}
45+
46+
ret
47+
}
48+
}
49+
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# 646. 最长数对链
2+
给你一个由 `n` 个数对组成的数对数组 `pairs` ,其中 <code>pairs[i] = [left<sub>i</sub>, right<sub>i</sub>]</code> 且 <code>left<sub>i</sub> < right<sub>i</sub></code> 。
3+
4+
现在,我们定义一种 **跟随** 关系,当且仅当 `b < c` 时,数对 `p2 = [c, d]` 才可以跟在 `p1 = [a, b]` 后面。我们用这种形式来构造 **数对链**
5+
6+
找出并返回能够形成的 **最长数对链的长度**
7+
8+
你不需要用到所有的数对,你可以以任何顺序选择其中的一些数对来构造。
9+
10+
#### 示例 1:
11+
<pre>
12+
<strong>输入:</strong> pairs = [[1,2],[2,3],[3,4]]
13+
<strong>输出:</strong> 2
14+
<strong>解释:</strong> 最长的数对链是 [1,2] -> [3,4] 。
15+
</pre>
16+
17+
#### 示例 2:
18+
<pre>
19+
<strong>输入:</strong> pairs = [[1,2],[7,8],[4,5]]
20+
<strong>输出:</strong> 3
21+
<strong>解释:</strong> 最长的数对链是 [1,2] -> [4,5] -> [7,8] 。
22+
</pre>
23+
24+
#### 提示:
25+
* `n == pairs.length`
26+
* `1 <= n <= 1000`
27+
* <code>-1000 <= left<sub>i</sub> < right<sub>i</sub> <= 1000</code>
28+
29+
## 题解 (Rust)
30+
31+
### 1. 题解
32+
```Rust
33+
impl Solution {
34+
pub fn find_longest_chain(mut pairs: Vec<Vec<i32>>) -> i32 {
35+
pairs.sort_unstable_by_key(|p| (p[1], -p[0]));
36+
let mut right = pairs[0][1];
37+
let mut ret = 1;
38+
39+
for i in 1..pairs.len() {
40+
if pairs[i][0] > right {
41+
right = pairs[i][1];
42+
ret += 1;
43+
}
44+
}
45+
46+
ret
47+
}
48+
}
49+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn find_longest_chain(mut pairs: Vec<Vec<i32>>) -> i32 {
3+
pairs.sort_unstable_by_key(|p| (p[1], -p[0]));
4+
let mut right = pairs[0][1];
5+
let mut ret = 1;
6+
7+
for i in 1..pairs.len() {
8+
if pairs[i][0] > right {
9+
right = pairs[i][1];
10+
ret += 1;
11+
}
12+
}
13+
14+
ret
15+
}
16+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@
391391
[641][641l] |[Design Circular Deque][641] |![rs]
392392
[643][643l] |[Maximum Average Subarray I][643] |![rs]
393393
[645][645l] |[Set Mismatch][645] |![rs]
394+
[646][646l] |[Maximum Length of Pair Chain][646] |![rs]
394395
[647][647l] |[Palindromic Substrings][647] |![rb]
395396
[648][648l] |[Replace Words][648] |![py]
396397
[652][652l] |[Find Duplicate Subtrees][652] |![py]
@@ -1791,6 +1792,7 @@
17911792
[641]:Problemset/0641-Design%20Circular%20Deque/README.md#641-design-circular-deque
17921793
[643]:Problemset/0643-Maximum%20Average%20Subarray%20I/README.md#643-maximum-average-subarray-i
17931794
[645]:Problemset/0645-Set%20Mismatch/README.md#645-set-mismatch
1795+
[646]:Problemset/0646-Maximum%20Length%20of%20Pair%20Chain/README.md#646-maximum-length-of-pair-chain
17941796
[647]:Problemset/0647-Palindromic%20Substrings/README.md#647-palindromic-substrings
17951797
[648]:Problemset/0648-Replace%20Words/README.md#648-replace-words
17961798
[652]:Problemset/0652-Find%20Duplicate%20Subtrees/README.md#652-find-duplicate-subtrees
@@ -3189,6 +3191,7 @@
31893191
[641l]:https://leetcode.com/problems/design-circular-deque/
31903192
[643l]:https://leetcode.com/problems/maximum-average-subarray-i/
31913193
[645l]:https://leetcode.com/problems/set-mismatch/
3194+
[646l]:https://leetcode.com/problems/maximum-length-of-pair-chain/
31923195
[647l]:https://leetcode.com/problems/palindromic-substrings/
31933196
[648l]:https://leetcode.com/problems/replace-words/
31943197
[652l]:https://leetcode.com/problems/find-duplicate-subtrees/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@
391391
[641][641l] |[设计循环双端队列][641] |![rs]
392392
[643][643l] |[子数组最大平均数 I][643] |![rs]
393393
[645][645l] |[错误的集合][645] |![rs]
394+
[646][646l] |[最长数对链][646] |![rs]
394395
[647][647l] |[回文子串][647] |![rb]
395396
[648][648l] |[单词替换][648] |![py]
396397
[652][652l] |[寻找重复的子树][652] |![py]
@@ -1791,6 +1792,7 @@
17911792
[641]:Problemset/0641-Design%20Circular%20Deque/README_CN.md#641-设计循环双端队列
17921793
[643]:Problemset/0643-Maximum%20Average%20Subarray%20I/README_CN.md#643-子数组最大平均数-i
17931794
[645]:Problemset/0645-Set%20Mismatch/README_CN.md#645-错误的集合
1795+
[646]:Problemset/0646-Maximum%20Length%20of%20Pair%20Chain/README_CN.md#646-最长数对链
17941796
[647]:Problemset/0647-Palindromic%20Substrings/README_CN.md#647-回文子串
17951797
[648]:Problemset/0648-Replace%20Words/README_CN.md#648-单词替换
17961798
[652]:Problemset/0652-Find%20Duplicate%20Subtrees/README_CN.md#652-寻找重复的子树
@@ -3189,6 +3191,7 @@
31893191
[641l]:https://leetcode.cn/problems/design-circular-deque/
31903192
[643l]:https://leetcode.cn/problems/maximum-average-subarray-i/
31913193
[645l]:https://leetcode.cn/problems/set-mismatch/
3194+
[646l]:https://leetcode.cn/problems/maximum-length-of-pair-chain/
31923195
[647l]:https://leetcode.cn/problems/palindromic-substrings/
31933196
[648l]:https://leetcode.cn/problems/replace-words/
31943197
[652l]:https://leetcode.cn/problems/find-duplicate-subtrees/

0 commit comments

Comments
 (0)