@@ -10,8 +10,9 @@ public static int longestBitonicSequence(int[] arr, int n) {
1010 //return solveRec(arr, 0, -1,false);
1111
1212 //
13- map =new HashMap <>();
14- return solveMemo (arr ,0 ,-1 ,false );
13+ // map =new HashMap<>();
14+ // return solveMemo(arr,0,-1,false);
15+ return solveLIS (arr );
1516 }
1617 public static int solveRec (int [] arr ,int indx ,int prev ,boolean flag ){
1718 //Base :::
@@ -36,7 +37,6 @@ public static int solveRec(int[] arr,int indx ,int prev,boolean flag){
3637 int notpick = solveRec (arr , indx +1 , prev , flag );
3738
3839 return Math .max (pick ,notpick );
39-
4040 }
4141 }
4242 public static int solveMemo (int [] arr ,int indx ,int prev ,boolean flag ){
@@ -68,4 +68,33 @@ public static int solveMemo(int[] arr,int indx ,int prev,boolean flag){
6868 return Math .max (pick ,notpick );
6969 }
7070 }
71+
72+ // Best Soln :: Apprcoch : Could be LIS (arr) LIS (rev(arr)) ::
73+ //Lets SEE ::
74+ public static int solveLIS (int arr []){
75+ int n =arr .length ;
76+ int frontLIS [] =new int [n ];
77+ int revLIS [] =new int [n ];
78+ Arrays .fill (frontLIS ,1 );
79+ Arrays .fill (revLIS ,1 );
80+ int maxi =0 ;
81+ for (int i =1 ;i <n ;i ++){
82+ for (int j =0 ;j <i ;j ++){
83+ if (arr [i ]>arr [j ] && 1 +frontLIS [j ]>frontLIS [i ]){
84+ frontLIS [i ] =1 +frontLIS [j ];
85+ }
86+ }
87+ }
88+
89+ for (int i =n -1 ;i >=0 ;i --){
90+ for (int j =n -1 ;j >i ;j --){
91+ if (arr [i ]>arr [j ] && 1 +revLIS [j ]>revLIS [i ]){
92+ revLIS [i ] =1 +revLIS [j ];
93+ }
94+ }
95+ maxi =Math .max (maxi ,frontLIS [i ]+revLIS [i ]-1 );
96+ }
97+ return maxi ;
98+ }
99+
71100}
0 commit comments