【leetcode】121. Best Time to Buy and Sell Stock

来源:互联网 发布:淘宝联盟没有券 编辑:程序博客网 时间:2024/05/16 06:16

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

/** * @param {number[]} prices * @return {number} */var maxProfit = function(prices) {    var ll = prices.length;    var value =0;    var min = prices[0];    if(ll<2){        return value;    }    for(var i=1;i<ll;i++){        var price = prices[i];        var diff = price - min;        if(diff>value){            value = diff;        }        if(price<min){            min=price;        }    }    return value;};
0 0
原创粉丝点击