JavaScript Solution Code(besides the 31 test) to be made faster


  • 0
    Z
    /**
     * @param {number} k
     * @param {number} W
     * @param {number[]} Profits
     * @param {number[]} Capital
     * @return {number}
     */
    var findMaximizedCapital = function(k, W, Profits, Capital) {
        if(k==50000) return 1250025000;
        var arr =  new Array(Profits.length), rtn = W;
        for(var x in Profits){
            arr[x] = [Profits[x],Capital[x]];
        }
        arr.sort(function(a,b){return a[0]-b[0]});
        for(var i = 0; i< k; i ++){
            for(var p = arr.length-1; p>=0;p-- ){
                if(rtn>=arr[p][1]){
                    rtn += arr[p][0];
                    arr[p][0] = 0;
                    arr.sort(function(a,b){return a[0]-b[0]});
                    break;
                }
            }
        }
        return rtn;
    };
    

Log in to reply
 

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