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

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

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

```/**
  * 
  * @param prices int整型一维数组 
  * @return int整型
  */
function maxProfit( prices ) {
    // write code here
    //首先判断是否有利益,只需要数组任意一个数小于后面的数即可,但是要注意时间复杂度
    //这里时间复杂度n,可以采用双层for循环解决,用max记录最大利益值,默认是0
    let max = 0
    for(let i=0;i<prices.length-1;i++){
        let temp = prices[i]
        for(let j=i+1;j<prices.length;j++){
            if(prices[j]>prices[i]){
                let diff=prices[j]-prices[i] 
            max=diff>max?diff:max
            }
        }
    }
    return max
}
module.exports = {
    maxProfit : maxProfit
};
全部评论

相关推荐

投递阿里巴巴控股集团等公司7个岗位 >
点赞 评论 收藏
转发
头像
04-29 10:53
已编辑
东北大学 自动化类
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务