-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindLongestChain.cpp
More file actions
31 lines (30 loc) · 979 Bytes
/
findLongestChain.cpp
File metadata and controls
31 lines (30 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
int findLongestChain(vector<vector<int>>& pairs) {
if (pairs.size() == 1) return 1;
sort(pairs.begin(), pairs.end());
vector<int> dp(pairs.size(), 1);
for (int i = pairs.size() - 2; i >= 0; --i) {
auto it = std::lower_bound(pairs.begin() + i + 1, pairs.end(), vector<int>{pairs[i][1] + 1, -1001});
if (it != pairs.end()) dp[i] = dp[it - pairs.begin()] + 1;
dp[i] = std::max(dp[i], dp[i + 1]);
}
return dp[0];
}
};
class Solution {
public:
int findLongestChain(vector<vector<int>>& pairs) {
sort(pairs.begin(), pairs.end(), [](const vector<int> &a, const vector<int> &b) {
return a[1] == b[1] ? a[0] > b[0] : a[1] < b[1];
});
int ans = 0, right = -1002;
for (auto &p: pairs) {
if (p[0] > right) {
++ans;
right = p[1];
}
}
return ans;
}
};