Skip to content

Commit 7d672de

Browse files
committed
feat: add solutions to lc problem: No.0646
No.0646.Maximum Length of Pair Chain
1 parent f5c6019 commit 7d672de

File tree

6 files changed

+359
-0
lines changed

6 files changed

+359
-0
lines changed

solution/0600-0699/0646.Maximum Length of Pair Chain/README.md

+159
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,181 @@
3434

3535
<!-- 这里可写通用的实现逻辑 -->
3636

37+
**方法一:动态规划**
38+
39+
先将 pairs 按照第一个数字升序排列,然后转换为最长上升子序列问题。
40+
41+
朴素做法,时间复杂度 O(n²)。
42+
43+
**方法二:贪心**
44+
45+
在所有可作为下一个数对的集合中,选择第二个数最小的数对添加到数对链。因此可以按照第二个数升序排列的顺序遍历所有数对,如果当前数能加入链,则加入。
46+
47+
时间复杂度 O(nlogn)。
48+
3749
<!-- tabs:start -->
3850

3951
### **Python3**
4052

4153
<!-- 这里可写当前语言的特殊实现逻辑 -->
4254

4355
```python
56+
class Solution:
57+
def findLongestChain(self, pairs: List[List[int]]) -> int:
58+
pairs.sort()
59+
dp = [1] * len(pairs)
60+
for i, (c, _) in enumerate(pairs):
61+
for j, (_, b) in enumerate(pairs[:i]):
62+
if b < c:
63+
dp[i] = max(dp[i], dp[j] + 1)
64+
return max(dp)
65+
```
4466

67+
```python
68+
class Solution:
69+
def findLongestChain(self, pairs: List[List[int]]) -> int:
70+
ans, cur = 0, float('-inf')
71+
for a, b in sorted(pairs, key=lambda x: x[1]):
72+
if cur < a:
73+
cur = b
74+
ans += 1
75+
return ans
4576
```
4677

4778
### **Java**
4879

4980
<!-- 这里可写当前语言的特殊实现逻辑 -->
5081

5182
```java
83+
class Solution {
84+
public int findLongestChain(int[][] pairs) {
85+
Arrays.sort(pairs, Comparator.comparingInt(a -> a[0]));
86+
int n = pairs.length;
87+
int[] dp = new int[n];
88+
int ans = 0;
89+
for (int i = 0; i < n; ++i) {
90+
dp[i] = 1;
91+
int c = pairs[i][0];
92+
for (int j = 0; j < i; ++j) {
93+
int b = pairs[j][1];
94+
if (b < c) {
95+
dp[i] = Math.max(dp[i], dp[j] + 1);
96+
}
97+
}
98+
ans = Math.max(ans, dp[i]);
99+
}
100+
return ans;
101+
}
102+
}
103+
```
104+
105+
```java
106+
class Solution {
107+
public int findLongestChain(int[][] pairs) {
108+
Arrays.sort(pairs, Comparator.comparingInt(a -> a[1]));
109+
int ans = 0;
110+
int cur = Integer.MIN_VALUE;
111+
for (int[] p : pairs) {
112+
if (cur < p[0]) {
113+
cur = p[1];
114+
++ans;
115+
}
116+
}
117+
return ans;
118+
}
119+
}
120+
```
121+
122+
### **C++**
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
int findLongestChain(vector<vector<int>>& pairs) {
128+
sort(pairs.begin(), pairs.end());
129+
int n = pairs.size();
130+
vector<int> dp(n, 1);
131+
for (int i = 0; i < n; ++i)
132+
{
133+
int c = pairs[i][0];
134+
for (int j = 0; j < i; ++j)
135+
{
136+
int b = pairs[j][1];
137+
if (b < c) dp[i] = max(dp[i], dp[j] + 1);
138+
}
139+
}
140+
return *max_element(dp.begin(), dp.end());
141+
}
142+
};
143+
```
144+
145+
```cpp
146+
class Solution {
147+
public:
148+
int findLongestChain(vector<vector<int>>& pairs) {
149+
sort(pairs.begin(), pairs.end(), [](vector<int> &a, vector<int>b) {
150+
return a[1] < b[1];
151+
});
152+
int ans = 0, cur = INT_MIN;
153+
for (auto& p : pairs)
154+
{
155+
if (cur < p[0])
156+
{
157+
cur = p[1];
158+
++ans;
159+
}
160+
}
161+
return ans;
162+
}
163+
};
164+
```
165+
166+
### **Go**
167+
168+
```go
169+
func findLongestChain(pairs [][]int) int {
170+
sort.Slice(pairs, func(i, j int) bool {
171+
return pairs[i][0] < pairs[j][0]
172+
})
173+
n := len(pairs)
174+
dp := make([]int, n)
175+
ans := 0
176+
for i := range pairs {
177+
dp[i] = 1
178+
c := pairs[i][0]
179+
for j := range pairs[:i] {
180+
b := pairs[j][1]
181+
if b < c {
182+
dp[i] = max(dp[i], dp[j]+1)
183+
}
184+
}
185+
ans = max(ans, dp[i])
186+
}
187+
return ans
188+
}
189+
190+
func max(a, b int) int {
191+
if a > b {
192+
return a
193+
}
194+
return b
195+
}
196+
```
52197

198+
```go
199+
func findLongestChain(pairs [][]int) int {
200+
sort.Slice(pairs, func(i, j int) bool {
201+
return pairs[i][1] < pairs[j][1]
202+
})
203+
ans, cur := 0, math.MinInt32
204+
for _, p := range pairs {
205+
if cur < p[0] {
206+
cur = p[1]
207+
ans++
208+
}
209+
}
210+
return ans
211+
}
53212
```
54213

55214
### **...**

solution/0600-0699/0646.Maximum Length of Pair Chain/README_EN.md

+147
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,160 @@
4545
### **Python3**
4646

4747
```python
48+
class Solution:
49+
def findLongestChain(self, pairs: List[List[int]]) -> int:
50+
pairs.sort()
51+
dp = [1] * len(pairs)
52+
for i, (c, _) in enumerate(pairs):
53+
for j, (_, b) in enumerate(pairs[:i]):
54+
if b < c:
55+
dp[i] = max(dp[i], dp[j] + 1)
56+
return max(dp)
57+
```
4858

59+
```python
60+
class Solution:
61+
def findLongestChain(self, pairs: List[List[int]]) -> int:
62+
ans, cur = 0, float('-inf')
63+
for a, b in sorted(pairs, key=lambda x: x[1]):
64+
if cur < a:
65+
cur = b
66+
ans += 1
67+
return ans
4968
```
5069

5170
### **Java**
5271

5372
```java
73+
class Solution {
74+
public int findLongestChain(int[][] pairs) {
75+
Arrays.sort(pairs, Comparator.comparingInt(a -> a[0]));
76+
int n = pairs.length;
77+
int[] dp = new int[n];
78+
int ans = 0;
79+
for (int i = 0; i < n; ++i) {
80+
dp[i] = 1;
81+
int c = pairs[i][0];
82+
for (int j = 0; j < i; ++j) {
83+
int b = pairs[j][1];
84+
if (b < c) {
85+
dp[i] = Math.max(dp[i], dp[j] + 1);
86+
}
87+
}
88+
ans = Math.max(ans, dp[i]);
89+
}
90+
return ans;
91+
}
92+
}
93+
```
94+
95+
```java
96+
class Solution {
97+
public int findLongestChain(int[][] pairs) {
98+
Arrays.sort(pairs, Comparator.comparingInt(a -> a[1]));
99+
int ans = 0;
100+
int cur = Integer.MIN_VALUE;
101+
for (int[] p : pairs) {
102+
if (cur < p[0]) {
103+
cur = p[1];
104+
++ans;
105+
}
106+
}
107+
return ans;
108+
}
109+
}
110+
```
111+
112+
### **C++**
113+
114+
```cpp
115+
class Solution {
116+
public:
117+
int findLongestChain(vector<vector<int>>& pairs) {
118+
sort(pairs.begin(), pairs.end());
119+
int n = pairs.size();
120+
vector<int> dp(n, 1);
121+
for (int i = 0; i < n; ++i)
122+
{
123+
int c = pairs[i][0];
124+
for (int j = 0; j < i; ++j)
125+
{
126+
int b = pairs[j][1];
127+
if (b < c) dp[i] = max(dp[i], dp[j] + 1);
128+
}
129+
}
130+
return *max_element(dp.begin(), dp.end());
131+
}
132+
};
133+
```
134+
135+
```cpp
136+
class Solution {
137+
public:
138+
int findLongestChain(vector<vector<int>>& pairs) {
139+
sort(pairs.begin(), pairs.end(), [](vector<int> &a, vector<int>b) {
140+
return a[1] < b[1];
141+
});
142+
int ans = 0, cur = INT_MIN;
143+
for (auto& p : pairs)
144+
{
145+
if (cur < p[0])
146+
{
147+
cur = p[1];
148+
++ans;
149+
}
150+
}
151+
return ans;
152+
}
153+
};
154+
```
155+
156+
### **Go**
157+
158+
```go
159+
func findLongestChain(pairs [][]int) int {
160+
sort.Slice(pairs, func(i, j int) bool {
161+
return pairs[i][0] < pairs[j][0]
162+
})
163+
n := len(pairs)
164+
dp := make([]int, n)
165+
ans := 0
166+
for i := range pairs {
167+
dp[i] = 1
168+
c := pairs[i][0]
169+
for j := range pairs[:i] {
170+
b := pairs[j][1]
171+
if b < c {
172+
dp[i] = max(dp[i], dp[j]+1)
173+
}
174+
}
175+
ans = max(ans, dp[i])
176+
}
177+
return ans
178+
}
179+
180+
func max(a, b int) int {
181+
if a > b {
182+
return a
183+
}
184+
return b
185+
}
186+
```
54187

188+
```go
189+
func findLongestChain(pairs [][]int) int {
190+
sort.Slice(pairs, func(i, j int) bool {
191+
return pairs[i][1] < pairs[j][1]
192+
})
193+
ans, cur := 0, math.MinInt32
194+
for _, p := range pairs {
195+
if cur < p[0] {
196+
cur = p[1]
197+
ans++
198+
}
199+
}
200+
return ans
201+
}
55202
```
56203

57204
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int findLongestChain(vector<vector<int>>& pairs) {
4+
sort(pairs.begin(), pairs.end(), [](vector<int> &a, vector<int>b) {
5+
return a[1] < b[1];
6+
});
7+
int ans = 0, cur = INT_MIN;
8+
for (auto& p : pairs)
9+
{
10+
if (cur < p[0])
11+
{
12+
cur = p[1];
13+
++ans;
14+
}
15+
}
16+
return ans;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func findLongestChain(pairs [][]int) int {
2+
sort.Slice(pairs, func(i, j int) bool {
3+
return pairs[i][1] < pairs[j][1]
4+
})
5+
ans, cur := 0, math.MinInt32
6+
for _, p := range pairs {
7+
if cur < p[0] {
8+
cur = p[1]
9+
ans++
10+
}
11+
}
12+
return ans
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int findLongestChain(int[][] pairs) {
3+
Arrays.sort(pairs, Comparator.comparingInt(a -> a[1]));
4+
int ans = 0;
5+
int cur = Integer.MIN_VALUE;
6+
for (int[] p : pairs) {
7+
if (cur < p[0]) {
8+
cur = p[1];
9+
++ans;
10+
}
11+
}
12+
return ans;
13+
}
14+
}

0 commit comments

Comments
 (0)