Skip to content

Commit 0fca5dc

Browse files
authoredOct 5, 2021
Create Russian-doll-envelops.cpp
1 parent 67a8a04 commit 0fca5dc

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
int Solution::solve(vector < vector < int > > & A) {
2+
vector < pair < int, int > > v;
3+
int n = A.size();
4+
for (int i = 0; i < n; i++)
5+
v.push_back({A[i][0], -A[i][1]});
6+
7+
// sort the vector envelope in increasing order of heights
8+
sort(v.begin(), v.end());
9+
int dp[n];
10+
dp[0] = 1;
11+
12+
//Find the longest increasing subsequence based on second element of v[i]
13+
for (int i = 1; i < n; i++) {
14+
dp[i] = 1;
15+
for (int j = i - 1; j >= 0; j--) {
16+
if (v[j].first < v[i].first and v[j].second > v[i].second)
17+
dp[i] = max(dp[i], dp[j] + 1);
18+
}
19+
}
20+
return *max_element(dp, dp + n);
21+
}

0 commit comments

Comments
 (0)
Please sign in to comment.