fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static int solve(int[] arr) {
  11. int n = arr.length;
  12. int result = 0;
  13. int currentMax = Integer.MIN_VALUE;
  14.  
  15. int size = 0;
  16. for (int i = 0; i < n; i++) {
  17. currentMax = Math.max(currentMax, arr[i]);
  18.  
  19. if (arr[i] == currentMax) {
  20. size++;
  21. int backUp = size;
  22. for(int j=i+1; j<n; j++) {
  23. if(arr[j] <= currentMax) {
  24. size++;
  25. } else {
  26. break;
  27. }
  28. }
  29. result += size;
  30. size = backUp;
  31. } else {
  32. size = 1;
  33. currentMax = arr[i];
  34. result++;
  35. }
  36. }
  37.  
  38. return result;
  39. }
  40.  
  41. public static void main(String[] args) {
  42. System.out.println(solve(new int[]{3,5,6}));//6
  43. System.out.println(solve(new int[]{1,2,1}));//5
  44. System.out.println(solve(new int[]{1,1,1,1}));//16
  45. System.out.println();
  46. System.out.println(solve(new int[]{6,5,3}));//6?
  47. }
  48. }
Success #stdin #stdout 0.08s 54652KB
stdin
Standard input is empty
stdout
6
5
16

5