Guest User

Untitled

a guest
Apr 30th, 2022
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // you can also use imports, for example:
  2. // import java.util.*;
  3.  
  4. // you can write to stdout for debugging purposes, e.g.
  5. // System.out.println("this is a debug message");
  6.  
  7. class Solution {
  8.     int maxElement = 0;
  9.     int currentMax = 0;
  10.     public int[] solution(int N, int[] A) {
  11.         int[] result = new int[N];
  12.         for(int i = 0; i < A.length; i++) {
  13.             if(A[i] >= 1 && A[i] <= N) {
  14.                 increase(A[i], result);
  15.                 maxElement = Math.max(result[A[i]-1], maxElement);
  16.             } else if (A[i] == N + 1) {
  17.                 currnetMax = maxElement;
  18.             }
  19.         }
  20.         for(int i = 0; i < result.length; i++) {
  21.              result[i] = Math.max(result[i], currentMax);
  22.         }
  23.         return result;
  24.     }
  25.     public void increase(int i, int[] j) {
  26.         j[i-1] = currentMax + j[i-1] + 1;
  27.     }
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment