Maximum Length of Pair Chain LeetCode solution in JavaScript

1
class Solution {
2
public:
3
int findLongestChain(vector<vector<int>>& arr) {
4

5
if(arr.size()==1)
6
{
7
return 1;
8
}
9
vector<pair<int,int>>v;
10
for(int i=0;i<arr.size();i++)
11
{
12
v.push_back({arr[i][0],arr[i][1]});
13
}
14
sort(v.begin(),v.end());
15

16
vector<int>dp(v.size(),0);
17
int ans=INT_MIN;
18
dp[0]=1;
19
for(int i=1;i<v.size();i++)
20
{
21
int temp=0;
22
for(int j=i-1;j>=0;j--)
23
{
24
if(v[j].second<v[i].first)
25
{
26
temp=max(temp,dp[j]);
27
}
28
}
29
dp[i]=1+temp;
30
ans=max(ans,dp[i]);
31
}
32
return ans;
33
}
34
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0