Java O(n) with Priority Queue, over 98% beats


  • 0
    A
    public int[][] reconstructQueue(int[][] people) {
      
        Arrays.sort(people, new Comparator<int[]>() {
                @Override
                public int compare(int[] o1, int[] o2) {
                    int res = Integer.compare(o2[0], o1[0]);
                	if (res == 0){
                    	return Integer.compare(o1[1], o2[1]);
                    }
                    else return res;
                }
            });
            ArrayList<int[]> queue = new ArrayList<int[]>();
            for (int[] person : people){
            	queue.add(person[1], person);
            	
            }
            return queue.toArray(new int[people.length][2]);
    }

Log in to reply
 

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