Leetcode 121. Best Time to Buy and Sell Stock javascript解决方案

337 阅读1分钟
/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function(prices) {
    let array = [];
    for(let i = 0; i < prices.length; ++i){
        array.push({
            ind: i,
            val: prices[i]
        });
    }
    array.sort((a, b) => b.val - a.val);
    let max = 0;
    for(let i = 0; i < array.length; ++i){
        for(let j = array.length - 1; j > i; --j){
            let obji = array[i];
            let objj = array[j];
            if(obji.ind > objj.ind){
                let tmp = obji.val - objj.val;
                if(tmp > max){
                    max = tmp;
                }
            }
        }
    }
    return max;
};