package longestmonotonicsubsequence; /** * Title: LongestMonotonicSubsequence * Description: O(n^2) dynamic programming alg to find longest subsequence * Copyright: Copyright (c) 2002 * @author Burt Rosenberg * @version 17 June 2002 */ public class LongestMonotonicSubsequence { public static void main ( String [] args ) { int [] x = { 3, 1, 5, 2, 3, 6, 5 } ; int [] a = dynamicProgramming( x ) ; printArray( "input array:", x ) ; printArray("longest subsequence:", longestSubsequence(x,a)) ; } static void printArray( String title, int [] a ) { System.out.println(title); for ( int i=0; imaxSoFar ) maxSoFar = a[j] ; } a[i] = maxSoFar + 1 ; } return a ; } static int lengthOfLongestSubsequence(int [] a ) { int maxSoFar = 0 ; for ( int i=0; imaxSoFar) maxSoFar = a[i] ; return maxSoFar ; } static int [] longestSubsequence(int [] x, int [] a ) { // reconstruct chain which is longest subsequence, working backwards int lols = lengthOfLongestSubsequence(a) ; int [] ss = new int[lols] ; int lastIndex ; for ( lastIndex=0; lastIndex=0; i-- ) { if ( x[i]