package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/previous-permutation-with-one-swap/
 */
public class Problem1053 {
    public int[] prevPermOpt1(int[] A) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem1053 prob = new Problem1053();
        System.out.println(Arrays.toString(prob.prevPermOpt1(new int[]{3, 2, 1}))); // [3,1,2]
        System.out.println(Arrays.toString(prob.prevPermOpt1(new int[]{1, 1, 5}))); // [1,1,5]
        System.out.println(Arrays.toString(prob.prevPermOpt1(new int[]{1, 9, 4, 6, 7}))); // [1,7,4,6,9]
        System.out.println(Arrays.toString(prob.prevPermOpt1(new int[]{3, 1, 1, 3}))); // [1,3,1,3]
    }
}

@@ -16,34 +16,8 @@
 e;%0A%0A
-import java.util.Arrays;%0A%0A
 /**%0A
@@ -169,529 +169,486 @@
     
-// TODO%0A        return null;
+for (int i = A.length - 1; i %3E= 0; i--) %7B
 %0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem1053 prob = new Problem1053();%0A        System.out.println(Arrays.toString(prob.prevPermOpt1(new int%5B%5D%7B3, 2, 1%7D))); // %5B3,1,2%5D%0A        System.out.println(Arrays.toString(prob.prevPermOpt1(new int%5B%5D%7B1, 1, 5%7D))); // %5B1,1,5%5D%0A        System.out.println(Arrays.toString(prob.prevPermOpt1(new int%5B%5D%7B1, 9, 4, 6, 7%7D))); // %5B1,7,4,6,9%5D%0A        System.out.println(Arrays.toString(prob.prevPermOpt1(new int%5B%5D%7B3, 1, 1, 3%7D))); // %5B1,3,1,3%5D
+    int j = i + 1;%0A            int index = -1;%0A            while (j %3C A.length && A%5Bi%5D %3E A%5Bj%5D) %7B%0A                if (index == -1 %7C%7C A%5Bindex%5D != A%5Bj%5D) %7B%0A                    index = j;%0A                %7D%0A                j++;%0A            %7D%0A            if (index != -1) %7B%0A                int tmp = A%5Bi%5D;%0A                A%5Bi%5D = A%5Bindex%5D;%0A                A%5Bindex%5D = tmp;%0A                break;%0A            %7D%0A        %7D%0A        return A;
 %0A   

Solve problem 1053