Advertisement
Guest User

plateau

a guest
Nov 29th, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1. public class LongestPlateau {
  2.     public static void main(String[] args) {
  3.      args = new String[] {"5", "5", "5", "5", "3", "8", "8", "8", "1"};
  4. //   args = new String[] {"1", "2", "2", "2", "2", "5", "5", "5", "3"};
  5. //  args = new String[] { "1", "2", "3", "4" };
  6.     int N = args.length;
  7.     int[] a = new int[N];
  8.  
  9.     for (int i = 0; i < N; i++) {
  10.         a[i] = Integer.parseInt(args[i]);
  11.     }
  12.  
  13.     boolean longestIsAtFront = true;
  14.     int currentLength = 0;
  15.     int longestLength = 0;
  16.     int plateauStart = 0;
  17.  
  18.     for (int i = 0; i < N; i++) {
  19.         if (a[plateauStart] == a[i]) {
  20.         currentLength++;
  21.         } else if (a[plateauStart] < a[i]) {
  22.         if (longestLength < currentLength) {
  23.             longestLength = currentLength;
  24.             longestIsAtFront = false;
  25.         }
  26.         currentLength = 0;
  27.         if (!longestIsAtFront) {
  28.             plateauStart = i;
  29.         }
  30.         } else {
  31.         if (longestLength < currentLength) {
  32.             longestLength = currentLength;
  33.         }
  34.         currentLength = 0;
  35.         }
  36.         System.out.println(plateauStart + " " + longestLength);
  37.     }
  38.     if (longestLength < currentLength) {
  39.         longestLength = currentLength;
  40.     }
  41.  
  42.     System.out.println(plateauStart + " " + longestLength);
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement