Java Solution, using sort and insertion.


  • 0
    L
        public int[][] reconstructQueue(int[][] people) {
            if(people.length == 0 || people[0].length == 0) return people;
            int n = people.length;
            Arrays.sort(people, new Comparator<int[]>(){
                public int compare(int[] a, int[] b){
                    if(a[0] == b[0]) 
                        return a[1] - b[1];
                    else 
                        return b[0] - a[0];
                }
            });
            List<int[]> list = new ArrayList();
            for(int i=0; i<n; i++){
                int pos = people[i][1];
                list.add(pos<=list.size()?pos:list.size(), people[i]);
            }
            return list.toArray(new int[n][2]);
        }
    

Log in to reply
 

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