Java Clear Solution Sort and Insert beats 99%


  • 1
    L
    public class Solution {
        public int[][] reconstructQueue(int[][] people) {
            if (people == null || people.length == 0 || people[0].length ==0) return people;
            
            Arrays.sort(people, new Comparator<int[]>(){
                @Override
                public int compare(int[] o1, int[] o2) {
                    if(o1[0] == o2[0]) return o1[1] - o2[1];
                    return o2[0] - o1[0];
                }
            });
            
            List<int[]> list = new ArrayList<>();
            
            for (int[] p: people) {
                list.add(p[1], p);
            }
            
            return list.toArray(new int[][]{});
        }
    }
    

Log in to reply
 

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