package leetcode;

/**
 * https://leetcode.com/problems/maximum-width-ramp/
 */
public class Problem962 {
    public int maxWidthRamp(int[] A) {
        int max = 0;
        for (int i = 0; i < A.length; i++) {
            for (int j = A.length - 1; j >= i; j--) {
                if (A[i] <= A[j]) {
                    max = Math.max(max, j - i);
                    break;
                }
            }
        }
        return max;
    }

    public static void main(String[] args) {
        Problem962 prob = new Problem962();
        System.out.println(prob.maxWidthRamp(new int[]{6, 0, 8, 2, 1, 5})); // 4
        System.out.println(prob.maxWidthRamp(new int[]{9, 8, 1, 0, 1, 9, 4, 0, 4, 1})); // 7
        System.out.println(prob.maxWidthRamp(new int[]{5, 4, 3, 2, 1})); // 0
    }
}

@@ -12,16 +12,71 @@
 tcode;%0A%0A
+import java.util.Arrays;%0Aimport java.util.Comparator;%0A%0A
 /**%0A * h
@@ -205,19 +205,49 @@
     
-int max = 0
+Integer%5B%5D indexes = new Integer%5BA.length%5D
 ;%0A  
@@ -305,49 +305,23 @@
     
-for (int j = A.length - 1; j %3E= i; j--) %7B
+indexes%5Bi%5D = i;
 %0A   
@@ -325,24 +325,26 @@
         
+%7D%0A
         
 if (A%5Bi%5D
@@ -339,512 +339,306 @@
     
-if (A%5Bi%5D %3C= A%5Bj%5D) %7B%0A                    max = Math.max(max, j - i);%0A                    break;%0A                %7D%0A            %7D%0A        %7D%0A        return max
+Arrays.sort(indexes, Comparator.comparingInt(index -%3E A%5Bindex%5D));%0A        int answer = 0;%0A        int minIndex = A.length
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem962 prob = new Problem962();%0A        System.out.println(prob.maxWidthRamp(new int%5B%5D%7B6, 0, 8, 2, 1, 5%7D)); // 4%0A        System.out.println(prob.maxWidthRamp(new int%5B%5D%7B9, 8, 1, 0, 1, 9, 4, 0, 4, 1%7D)); // 7%0A        System.out.println(prob.maxWidthRamp(new int%5B%5D%7B5, 4, 3, 2, 1%7D)); // 0
+for (int index : indexes) %7B%0A            answer = Math.max(answer, index - minIndex);%0A            minIndex = Math.min(minIndex, index);%0A        %7D%0A        return answer;
 %0A   

Solve problem 962