package leetcode;

/**
 * https://leetcode.com/problems/queue-reconstruction-by-height/
 */
public class Problem406 {
    public int[][] reconstructQueue(int[][] people) {
        // TODO
        return null;
    }

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

@@ -12,16 +12,124 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.List;%0Aimport java.util.TreeSet;%0A%0A
 /**%0A * h
@@ -285,130 +285,598 @@
     
-// TODO%0A        return null;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem406 prob = new Problem406(
+List%3CHeight%3E heights = new ArrayList%3C%3E();%0A        for (int i = 0; i %3C people.length; i++) %7B%0A            int h = people%5Bi%5D%5B0%5D;%0A            int k = people%5Bi%5D%5B1%5D;%0A            heights.add(new Height(h, k));%0A        %7D%0A        Collections.sort(heights, (a, b) -%3E %7B%0A            if (a.h == b.h) %7B%0A                return Integer.compare(b.k, a.k);%0A            %7D%0A            return Integer.compare(a.h, b.h);%0A        %7D);%0A        int%5B%5D%5B%5D result = new int%5Bpeople.length%5D%5B2%5D;%0A        TreeSet%3CInteger%3E indices = new TreeSet%3C%3E();%0A        for (int i = 0; i %3C people.length; i++) %7B%0A            indices.add(i
 );%0A 
@@ -885,171 +885,662 @@
     
+ 
- System.out.println(prob.reconstructQueue(%0A            new int%5B%5D%5B%5D%7B%7B7, 0%7D, %7B4, 4%7D, %7B7, 1%7D, %7B5, 0%7D, %7B6, 1%7D, %7B5, 2%7D%7D)); // %5B%5B5,0%5D, %5B7,0%5D, %5B5,2%5D, %5B6,1%5D, %5B4,4%5D, %5B7,1%5D%5D
+%7D%0A        for (Height height : heights) %7B%0A            int target = height.k;%0A            int idx = 0;%0A            int step = 0;%0A            for (int i : indices) %7B%0A                if (step == target) %7B%0A                    idx = i;%0A                    break;%0A                %7D%0A                step++;%0A            %7D%0A            result%5Bidx%5D%5B0%5D = height.h;%0A            result%5Bidx%5D%5B1%5D = height.k;%0A            indices.remove(idx);%0A        %7D%0A        return result;%0A    %7D%0A%0A    private static class Height %7B%0A        private final int h;%0A        private final int k;%0A%0A        public Height(int h, int k) %7B%0A            this.h = h;%0A            this.k = k;%0A        %7D
 %0A   

Solve problem 406