Advertisement
Viksy

JumpSol

Sep 4th, 2022
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. public static void solution(int n, int[] nums) {
  2.         Stack<Integer> stack = new Stack<>();
  3.  
  4.         int highestJump = 0;
  5.         int[] jumpPerNum = new int[n];
  6.  
  7.         for(int i = 0; i < n; i++){
  8.             int currNum = i;
  9.             stack.push(nums[currNum]);
  10.  
  11.             int count = n - 1;
  12.             while (currNum < count) {
  13.                 if(nums[currNum + 1] > stack.peek()) {
  14.                     stack.push(nums[currNum + 1]);
  15.                 }
  16.                 if(nums[count] < stack.peek()) {
  17.                     count--;
  18.                 }
  19.                 currNum++;
  20.             }
  21.  
  22.             jumpPerNum[i] = stack.size() - 1;
  23.  
  24.             if(highestJump < jumpPerNum[i]) {
  25.                 highestJump = jumpPerNum[i];
  26.             }
  27.  
  28.             stack.clear();
  29.         }
  30.  
  31.         System.out.println(highestJump);
  32.         System.out.println(Arrays.toString(jumpPerNum));
  33.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement