|
34 | 34 |
|
35 | 35 | <!-- 这里可写通用的实现逻辑 -->
|
36 | 36 |
|
| 37 | +**方法一:动态规划** |
| 38 | + |
| 39 | +先将 pairs 按照第一个数字升序排列,然后转换为最长上升子序列问题。 |
| 40 | + |
| 41 | +朴素做法,时间复杂度 O(n²)。 |
| 42 | + |
| 43 | +**方法二:贪心** |
| 44 | + |
| 45 | +在所有可作为下一个数对的集合中,选择第二个数最小的数对添加到数对链。因此可以按照第二个数升序排列的顺序遍历所有数对,如果当前数能加入链,则加入。 |
| 46 | + |
| 47 | +时间复杂度 O(nlogn)。 |
| 48 | + |
37 | 49 | <!-- tabs:start -->
|
38 | 50 |
|
39 | 51 | ### **Python3**
|
40 | 52 |
|
41 | 53 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
42 | 54 |
|
43 | 55 | ```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 | +``` |
44 | 66 |
|
| 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 |
45 | 76 | ```
|
46 | 77 |
|
47 | 78 | ### **Java**
|
48 | 79 |
|
49 | 80 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
50 | 81 |
|
51 | 82 | ```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 | +``` |
52 | 197 |
|
| 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 | +} |
53 | 212 | ```
|
54 | 213 |
|
55 | 214 | ### **...**
|
|
0 commit comments