Advertisement
bkit4s0

[find longest subsequence]

Jul 3rd, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. /**
  2.  *
  3.  * @author John
  4.  *
  5.  */
  6. public class type {
  7.  
  8.     public static void main(String[] args) {
  9.         int[] testcase = {3,2,6,4,5};
  10.         int result = findLongestIncreasingSubsequence(testcase);
  11.         System.out.println(result);
  12.     }
  13.     /**
  14.      *
  15.      * @param input
  16.      * @return
  17.      */
  18.     private static int findLongestIncreasingSubsequence(int[] input) {
  19.         // TODO Auto-generated method stub
  20.         int inputLength = input.length;
  21.         int[] longestSubsequence = new int[inputLength];
  22.         for (int i = 0; i < inputLength; i++) {
  23.             longestSubsequence[i] = 1;
  24.             for (int j = 0; j < i; j++) {
  25.                 if(input[i] > input[j] && longestSubsequence[i] < longestSubsequence[j])
  26.                     longestSubsequence[i] = longestSubsequence[j] + 1;
  27.             }
  28.         }
  29.         int largest = 0;
  30.         for (int i = 0; i < inputLength; i++) {
  31.             if (largest < longestSubsequence[i]) {
  32.                 largest = longestSubsequence[i];
  33.             }
  34.         }
  35.         return largest;
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement