Longest Increasing Subsequence using Longest Common Subsequence Algorithm | C # implementation of above approach ; Function to return the size of the longest increasing subsequence ; Create an 2D array of integer for tabulation ; Take the second sequence as the sorted sequence of the given sequence ; Classical Dynamic Programming algorithm for longest Common Subsequence ; Return the ans ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int LISusingLCS ( List < int > seq ) { int n = seq . Count ; int [ , ] L = new int [ n + 1 , n + 1 ] ; List < int > sortedseq = new List < int > ( seq ) ; sortedseq . Sort ( ) ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i , j ] = 0 ; else if ( seq [ i - 1 ] == sortedseq [ j - 1 ] ) L [ i , j ] = L [ i - 1 , j - 1 ] + 1 ; else L [ i , j ] = Math . Max ( L [ i - 1 , j ] , L [ i , j - 1 ] ) ; } } return L [ n , n ] ; } public static void Main ( String [ ] args ) { List < int > sequence = new List < int > ( ) ; sequence . Add ( 12 ) ; sequence . Add ( 34 ) ; sequence . Add ( 1 ) ; sequence . Add ( 5 ) ; sequence . Add ( 40 ) ; sequence . Add ( 80 ) ; Console . WriteLine ( LISusingLCS ( sequence ) ) ; } }