-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_MaxLengthChain.cpp
104 lines (86 loc) · 1.77 KB
/
GFG_MaxLengthChain.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
https://practice.geeksforgeeks.org/problems/max-length-chain/1
Max length chain
*/
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
struct val{
int first;
int second;
};
int maxChainLen(struct val p[],int n);
int main() {
// your code goes here
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
val p[n];
for(int i=0;i<n;i++)
{
cin>>p[i].first>>p[i].second;
}
cout<<maxChainLen(p,n)<<endl;
}
return 0;
}// } Driver Code Ends
/*
The structure to use is as follows
struct val{
int first;
int second;
};*/
bool compare(const val &a, const val &b)
{
if(a.second == b.second) return a.first < b.first;
return a.second < b.second;
}
/*You are required to complete this method*/
int maxChainLen1(struct val p[],int n)
{
// auto compare1 = [](const val &a, const val &b){
// return a.first < b.first;
// };
sort(p, p+n, compare);
vector<struct val> chains;
chains.push_back(p[0]);
for(int i=1; i<n; i++)
{
if(chains.back().second < p[i].first )
{
chains.push_back(p[i]);
}
}
return chains.size();
}
int maxChainLen(struct val p[],int n)
{
auto compare1 = [](const val &a, const val &b){
return a.first < b.first;
};
sort(p, p+n, compare1);
vector<int> dp(n, 1);
int max=0;
for(int i=1; i<n; i++)
{
for(int j=0; j<i; j++)
{
if(p[j].second < p[i].first && dp[j]+1 > dp[i])
{ dp[i] = dp[j]+1;
if(dp[i]>max) max = dp[i];
}
}
}
for(int i=0; i<n; i++)
{
cout<<p[i].first<<" "<<p[i].second<<endl;
}
for(int i=0; i<n; i++)
{
cout<<"i="<<i<<" "<<dp[i]<<endl;
}
return max;
}