File tree 2 files changed +50
-169
lines changed
2 files changed +50
-169
lines changed Original file line number Diff line number Diff line change
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
+
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments