Skip to content

Commit 297940c

Browse files
authoredOct 1, 2021
Update and rename Longest Bitonic subsequence.md to Longest Bitonic subsequence.cpp
1 parent ff2b979 commit 297940c

File tree

2 files changed

+50
-169
lines changed

2 files changed

+50
-169
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Question Link : https://practice.geeksforgeeks.org/problems/longest-bitonic-subsequence0824/1#
2+
3+
4+
class Solution{
5+
public:
6+
int LongestBitonicSequence(vector<int>arr)
7+
{
8+
int n = arr.size();
9+
int dp[n]={0};
10+
11+
for(int i=0;i<n;i++){
12+
int max =0;
13+
14+
for(int j=0;j<i;j++){
15+
if(arr[j]<arr[i]){
16+
if(dp[j]>max){
17+
max = dp[j];
18+
}
19+
}
20+
}
21+
22+
dp[i] = max +1;
23+
}
24+
25+
26+
int lds[n]={0};
27+
for(int i=n-1;i>=0;i--){
28+
int max =0;
29+
30+
for(int j=n-1;j>i;j--){
31+
if(arr[j]<arr[i]){
32+
if(lds[j]>max){
33+
max = lds[j];
34+
}
35+
}
36+
}
37+
lds[i] = max +1;
38+
}
39+
40+
int omax=0;
41+
for(int i=0;i<n;i++){
42+
if((dp[i]+lds[i])-1>omax){
43+
omax = (dp[i]+lds[i])-1;
44+
}
45+
}
46+
47+
return omax;
48+
}
49+
};
50+

‎20. Dynamic Programming/Longest Bitonic subsequence.md

-169
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.