Advertisement
DNNdrago

4. Longest Increasing Sequence

May 23rd, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class _04_LongestIncreasingSequence {
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner sc = new Scanner (System.in);
  10.         String inputLine = sc.nextLine();
  11.         String[] inputNumbers = inputLine.split(" ");
  12.         int[] numbers = new int[inputNumbers.length + 1];
  13.        
  14.         for (int i = 0; i < inputNumbers.length; i++) {
  15.             numbers[i] = Integer.parseInt(inputNumbers[i]);
  16.         }
  17.        
  18.         numbers[numbers.length - 1] = Integer.MIN_VALUE;
  19.  
  20.         int longestSequence = 1;
  21.         int currentSequnce = 1;
  22.         for (int i = 0; i < numbers.length - 1; i++) {
  23.             if (numbers[i] < numbers[i+1]) {
  24.                 currentSequnce++;
  25.                 System.out.print(numbers[i] + " ");
  26.             }
  27.             else {
  28.                 System.out.println(numbers[i]);
  29.                 if (longestSequence < currentSequnce) {
  30.                     longestSequence = currentSequnce;
  31.                 }
  32.                 currentSequnce = 1;
  33.             }
  34.         }
  35.        
  36.         for (int i = 0; i < numbers.length - 1; i++) {
  37.             if (numbers[i] < numbers[i+1]) {
  38.                 currentSequnce++;
  39.             }
  40.             else {
  41.                 if (longestSequence == currentSequnce) {
  42.                     System.out.print("Longest: ");
  43.                     for(int j = i - longestSequence + 1; j <= i; j++) {
  44.                         System.out.print(numbers[j] + " ");
  45.                     }
  46.                     break;
  47.                 }
  48.                 currentSequnce = 1;
  49.             }
  50.         }
  51.        
  52.        
  53.     }
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement