Advertisement
Coriic

Untitled

Apr 27th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. public class Solution{
  2.     private static int INCREASING = 1;
  3.     private static int DECREASING = 2;
  4.     private static int CONSTANT = 3;
  5.  
  6.     public int solution(int[] A){
  7.         int currentMonotonousness = checkTypeOfSubsequence(0, A);
  8.         int lengthOfSubsequence = 1;
  9.         int numberOfSubseqences = 0;
  10.         for(int i = 0; i < A.length-1; i++){
  11.             if (checkIfNumberOfSubseqnecuesIsToBig(numberOfSubseqences)) return -1;
  12.             if(!isChangeOfMonotonousness(currentMonotonousness, i, A)){
  13.                 lengthOfSubsequence++;
  14.             }
  15.             else{
  16.                 numberOfSubseqences += calculateNumberOfSubseqnuences(lengthOfSubsequence);
  17.                 currentMonotonousness = checkTypeOfSubsequence(i, A);
  18.                 if(!isEndOfArray(i, A.length)) lengthOfSubsequence = 2;
  19.             }
  20.         }
  21.         numberOfSubseqences += calculateNumberOfSubseqnuences(lengthOfSubsequence);
  22.         return numberOfSubseqences;
  23.     }
  24.  
  25.     private boolean isChangeOfMonotonousness(int currentMonotonousness, int index, int[] array){
  26.         return currentMonotonousness != checkTypeOfSubsequence(index, array);
  27.     }
  28.  
  29.     private boolean isEndOfArray(int index, int arrayLength){
  30.         return index == arrayLength - 2;
  31.     }
  32.  
  33.     private int checkTypeOfSubsequence(int index, int[] array){
  34.         int differential = array[index+1] - array[index];
  35.         if(differential > 0){
  36.             return INCREASING;
  37.         }
  38.         else if(differential < 0){
  39.             return DECREASING;
  40.         }
  41.         else
  42.             return CONSTANT;
  43.     }
  44.  
  45.     private int calculateNumberOfSubseqnuences(int length){
  46.         int result = 0;
  47.         for(int i = length - 1; i > 0; i--){
  48.             result += i;
  49.         }
  50.         return result;
  51.     }
  52.  
  53.     private boolean checkIfNumberOfSubseqnecuesIsToBig(int sum){
  54.         return sum > 1000000000;
  55.     }
  56.  
  57.     public static void main(String[] args){
  58.         Solution solution = new Solution();
  59.         int result = solution.solution(new int[]{1,3,4,4,5,5,0});
  60.         System.out.println(result);
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement