406.Queue Reconstruction by Height

1- Greedy

Need Review

https://leetcode.com/problems/queue-reconstruction-by-height/discuss/89345/

class Solution {
    public int[][] reconstructQueue(int[][] people) {
        //pick up the tallest guy first
        //when insert the next tall guy, just need to insert him into kth position
        //repeat until all people are inserted into list
        Arrays.sort(people, new Comparator<int[]>(){
            @Override
            public int compare(int[] a, int[] b){
                // DESC 
                if(a[0] != b[0]){
                    return b[0] - a[0];
                }
                // ASC
                else{
                    return a[1] - b[1];
                }
            }
        });
        List<int[]> res = new LinkedList<>();
        for(int[] cur : people){
            res.add(cur[1], cur);
        }
        return res.toArray(new int[people.length][]);
    }
}

results matching ""

    No results matching ""