Very Concise Java Solution Based on Sort and Position Insertion

  • 0

    The idea is very simple: sort the hight in descending order, if the hight is same, then sort by position in ascending order. After sort, we insert each element of the array into a list according to their position.

    public int[][] reconstructQueue(int[][] people) {
         Arrays.sort(people, (o1, o2) -> o1[0] == o2[0] ? o1[1] - o2[1] : o2[0] - o1[0]);
         List<int[]> temp = new ArrayList<>();
         for (int[] ele : people) {
                temp.add(ele[1], ele);
           return temp.toArray(people);

Log in to reply

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