-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathMaximum Length of Pair Chain.cpp
40 lines (26 loc) · 1.15 KB
/
Maximum Length of Pair Chain.cpp
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
32
33
34
35
36
37
38
39
40
/*
Solution by Rahul Surana
***********************************************************
You are given an array of n pairs pairs where pairs[i] = [lefti, righti] and lefti < righti.
A pair p2 = [c, d] follows a pair p1 = [a, b] if b < c. A chain of pairs can be formed in this fashion.
Return the length longest chain which can be formed.
You do not need to use up all the given intervals. You can select pairs in any order.
***********************************************************
*/
class Solution {
public:
vector<int> dp;
int df(int i,vector<vector<int>>& pairs, int a){
if(i > pairs.size()-1) return 0;
if(dp[i] != -1) return dp[i];
// cout << pairs[i][0] <<" "<<pairs[i][1]<<" "<<a<<"\n";
if(pairs[i][0] > a)
return dp[i] = max( 1 + df(i+1,pairs,pairs[i][1]), df(i+1,pairs,a));
return dp[i] = df(i+1,pairs,a);
}
int findLongestChain(vector<vector<int>>& pairs) {
dp.resize(pairs.size()+1,-1);
sort(pairs.begin(),pairs.end(),[&](auto a,auto b){ return a[1] < b[1]; });
return df(0,pairs,-1001);
}
};