Short Javascript using Greedy


  • 0
    E
    function sortNumber(a,b)
    {
        return a[0] === b[0]? a[1] - b[1]: b[0] - a[0];
    }
     
    var reconstructQueue = function(people) {
        var sorted = [];
        people.sort(sortNumber);
        for(var i = 0; i < people.length; i++){
            sorted = sorted.pushQueue(people[i]);
        }
        return sorted;
    };
    
    Array.prototype.pushQueue = function(n){
        this.splice(n[1],0,n);
        return this;
    };
    

    The idea is always put the tallest one to the array, if more than one people with same height, put the one with less second number first.


Log in to reply
 

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