Using sort and insert in java


  • 0
    F

    first, sort by height h decreasingly and numbers k increasingly, then perform insert, the k value is the insertion position

        public int[][] reconstructQueue(int[][] people) {
            Arrays.sort(people, (o1, o2) -> o1[0] != o2[0] ? Integer.compare(o2[0], o1[0]) : Integer.compare(o1[1], o2[1]));
            ArrayList<int[]> result = new ArrayList<>();
            for (int[] person : people) {
                result.add(person[1], person);
            }
            return result.toArray(new int[result.size()][]);
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.