Easy to understand 6 lines of code in java


  • 1
    A
    • Sort the people in decreasing order of heights - h(primary condition) and increasing order of count of tall people ahead - k(secondary condition)
    • For each person from left to right, move them to position k of current array
        public int[][] reconstructQueue(int[][] people) {
            Arrays.sort(people,(x,y) -> (x[0] == y[0])?x[1]-y[1]:y[0]-x[0]);
            for(int i=1;i<people.length;i++) {
                int index = people[i][1];
                for(int j=i;j>index;j--)
                    swap(people, j, j-1);
            }
            return people;
        }
    

Log in to reply
 

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