package leetcode;

/**
 * https://leetcode.com/problems/minimum-adjacent-swaps-to-reach-the-kth-smallest-number/
 */
public class Problem1850 {
    public int getMinSwaps(String num, int k) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1850 prob = new Problem1850();
        System.out.println(prob.getMinSwaps("5489355142", 4)); // 2
        System.out.println(prob.getMinSwaps("11112", 4)); // 4
        System.out.println(prob.getMinSwaps("00123", 1)); // 1
    }
}

@@ -197,15 +197,1048 @@
     
-// TODO
+System.out.println(num);%0A        String str = getKthSmallest(num, k);%0A        System.out.println(str);%0A        return 0;%0A    %7D%0A%0A    private String getKthSmallest(String num, int k) %7B%0A        String str = num;%0A        char%5B%5D chars = num.toCharArray();%0A        for (int i = 0; i %3C k; i++) %7B%0A            int j = chars.length - 1;%0A            while (j - 1 %3E= 0 && chars%5Bj - 1%5D %3E= chars%5Bj%5D) %7B%0A                j--;%0A            %7D%0A            int indexToSwap = j - 1;%0A            int smallestLargerIndex = -1;%0A            for (int l = j; l %3C chars.length; l++) %7B%0A                if (chars%5BindexToSwap%5D %3C chars%5Bl%5D) %7B%0A                    if (smallestLargerIndex == -1) %7B%0A                        smallestLargerIndex = l;%0A                    %7D else if (chars%5BsmallestLargerIndex%5D %3E= chars%5Bl%5D) %7B%0A                        smallestLargerIndex = l;%0A                    %7D%0A                %7D%0A            %7D%0A            swap(chars, indexToSwap, smallestLargerIndex);%0A            reverse(chars, j, chars.length - 1);%0A            str = new String(chars);%0A        %7D
 %0A   
@@ -1241,33 +1241,35 @@
 %0A        return 
-0
+str
 ;%0A    %7D%0A%0A    pub
@@ -1257,24 +1257,332 @@
 str;%0A    %7D%0A%0A
+    private static void swap(char%5B%5D chars, int i, int j) %7B%0A        char tmp = chars%5Bi%5D;%0A        chars%5Bi%5D = chars%5Bj%5D;%0A        chars%5Bj%5D = tmp;%0A    %7D%0A%0A    private static void reverse(char%5B%5D chars, int i, int j) %7B%0A        for (int a = i, b = j; a %3C b; a++, b--) %7B%0A            swap(chars, a, b);%0A        %7D%0A    %7D%0A%0A
     public s
@@ -1728,16 +1728,86 @@
 ); // 2%0A
+        System.out.println(prob.getMinSwaps(%225489355142%22, 12)); // 11%0A
         

Update problem 1850