Viksy

jump

Sep 4th, 2022
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 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 highestNumber = Arrays.stream(nums).max().getAsInt();
  6. int[] jumpPerNum = new int[n];
  7.  
  8. for(int i = 0; i < n; i++){
  9. int currNum = i;
  10. stack.push(nums[currNum]);
  11.  
  12. int count = n - 1;
  13. while (currNum < count) {
  14. if(nums[currNum] == highestNumber) {
  15. break;
  16. }
  17. if(nums[currNum + 1] > stack.peek()) {
  18. stack.push(nums[currNum + 1]);
  19. }
  20. if(nums[count] < stack.peek()) {
  21. count--;
  22. }
  23.  
  24. currNum++;
  25. }
  26.  
  27. jumpPerNum[i] = stack.size() - 1;
  28.  
  29. if(highestJump < jumpPerNum[i]) {
  30. highestJump = jumpPerNum[i];
  31. }
  32.  
  33. stack.clear();
  34. }
  35.  
  36. System.out.println(highestJump);
  37. System.out.println(Arrays.toString(jumpPerNum));
  38. }
Advertisement
Add Comment
Please, Sign In to add comment