题解 | NC7 买卖股票的最好时机(一)

买卖股票的最好时机(一)

http://www.nowcoder.com/practice/64b4262d4e6d4f6181cd45446a5821ec

贪心法

  * 
  * @param prices int整型一维数组 
  * @return int整型
  */
function maxProfit( prices ) {
    // write code here
    let n = prices.length;
    if(n <=1)    return 0;
    let minValue = prices[0],maxP = 0;
    for(let i = 0;i < n;i++){
        minValue = Math.min(minValue,prices[i]);
        maxP = Math.max(maxP,prices[i] - minValue);
    }
    return maxP;
}
module.exports = {
    maxProfit : maxProfit
};
全部评论

相关推荐

不亏是提前批,神仙打架,鼠鼠不配了
站队站对牛:现在92都报工艺岗了
投递韶音科技等公司7个岗位
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-07 11:30
仁者伍敌:kpi都懒得刷了属于是
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务