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. import java.util.Arrays;
  8. import java.util.List;
  9.  
  10. /* Name of the class has to be "Main" only if the class is public. */
  11. class Ideone
  12. {
  13. public static int getMaxStableSegments(List<Integer> massList) {
  14.  
  15. int stableCount = 0;
  16. int ref = 0;
  17. int lastStableIndex = massList.size() - 1;
  18.  
  19. while (lastStableIndex > 0) {
  20. for (int i = lastStableIndex; i >=0 ; i--) {
  21. if(ref == 0) {
  22. ref = massList.get(i);
  23. lastStableIndex = i;
  24. } else if(massList.get(i) > ref) {
  25.  
  26. System.out.println(massList.get(i) + " " + ref);
  27.  
  28. stableCount++;
  29. ref = 0;
  30. lastStableIndex = i-1;
  31. }
  32. }
  33. if(stableCount == 0) {
  34. break;
  35. }
  36. lastStableIndex--;
  37. ref = 0;
  38. }
  39.  
  40. return stableCount;
  41. }
  42.  
  43.  
  44. public static void main (String[] args) throws java.lang.Exception
  45. {
  46. // your code goes here
  47.  
  48. System.out.println(getMaxStableSegments(Arrays. asList(4, 3, 6, 5, 3, 4, 7, 1))); // Output: 3
  49.  
  50. }
  51. }
Success #stdin #stdout 0.13s 57376KB
stdin
Standard input is empty
stdout
7 1
5 4
4 3
3