Advertisement
dimipan80

Longest Increasing Subsequence (LIS)

Jun 18th, 2017
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. /*  Read a list of integers and find the longest increasing subsequence (LIS). If several such exist, print the leftmost.
  2. *   Hints:
  3. *   • Assume we have n numbers in an array numbers[0…n-1].
  4. *   • Let lengthArr[p] holds the length of the longest increasing subsequence (LIS) ending at position p.
  5. *   • In a for loop, we shall calculate length[p] for p = 0 … n-1 as follows:
  6. *         Let left is the leftmost position on the left of p (left < p), such that lengthArr[left] is the largest possible.
  7. *         Then, lengthArr[p] = 1 + lengthArr[left]. If left does not exist, lengthArr[p] = 1.
  8. *         Also, save prevIndxArr[p] = left (we hold if prevIndxArr[] the previous position, used to obtain the best length for position p).
  9. *       Once the values for lengthArr[0…n-1] are calculated, restore the LIS starting from position p such that lengthArr[p] is maximal
  10. *       and go back and back through p = prevIndxArr[p].
  11. *
  12. *        Examples:
  13. *    [1]  ->   [1];   [7 3 5 8 -1 0 6 7]   ->   [3 5 6 7];     [1 2 5 3 5 2 4 1]   ->    [1 2 3 5];
  14. *    [0 10 20 30 30 40 1 50 2 3 4 5 6]   ->   [0 1 2 3 4 5 6];    [3 14 5 12 15 7 8 9 11 10 1]   ->   [3 5 7 8 9 11];
  15. *    [11 12 13 3 14 4 15 5 6 7 8 7 16 9 8]   ->   [3 4 5 6 7 8 16];     [7 7 7]   ->   [7];
  16. */
  17.  
  18.  
  19. import java.util.Arrays;
  20. import java.util.Scanner;
  21.  
  22. public class LIS {
  23.     public static void main(String[] args) {
  24.         Scanner scanner = new Scanner(System.in);
  25.         String[] arr = scanner.nextLine().split("\\s+");
  26.         scanner.close();
  27.  
  28.         int[] numbers = Arrays.stream(arr)
  29.                 .mapToInt(Integer::parseInt)
  30.                 .toArray();
  31.  
  32.         int[] lengthArr = new int[numbers.length];
  33.         int[] prevIndxArr = new int[numbers.length];
  34.         int maxLength = 0;
  35.         int lastIndex = -1;
  36.         for (int i = 0; i < numbers.length; i++) {
  37.             lengthArr[i] = 1;
  38.             prevIndxArr[i] = -1;
  39.             for (int j = 0; j < i; j++) {
  40.                 if ((numbers[j] < numbers[i]) && (lengthArr[j] + 1 > lengthArr[i])) {
  41.                     lengthArr[i] = lengthArr[j] + 1;
  42.                     prevIndxArr[i] = j;
  43.                 }
  44.  
  45.             }
  46.  
  47.             if (lengthArr[i] > maxLength) {
  48.                 maxLength = lengthArr[i];
  49.                 lastIndex = i;
  50.             }
  51.         }
  52.  
  53.         int[] longestSeq = new int[maxLength];
  54.         for (int i = maxLength - 1; i >= 0; i--) {
  55.             longestSeq[i] = numbers[lastIndex];
  56.             lastIndex = prevIndxArr[lastIndex];
  57.         }
  58.  
  59.         for (int num : longestSeq) {
  60.             System.out.printf("%d ", num);
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement