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

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

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

/**
  * 
  * @param prices int整型一维数组 
  * @return int整型
  */
// function maxProfit( prices ) {
//     // write code here
//     if(prices.length<2)return 0;
//     let dp=new Array(prices.length).fill(new Array(2).fill(0))
      //请注意这样创建的js二维数组 是会产生问题的  会一列跟着改变 最好还是两次for循环创建把
//     console.log(dp)
//     dp[0][0]=0
//     dp[0][1]=-prices[0]
//         console.log(dp)

//     for(let i=1;i<prices.length;i++){
//         dp[i][0]=Math.max(dp[i-1][0],prices[i]+dp[i-1][1])
//         dp[i][1]=Math.max(dp[i-1],[1],-prices[i])
//     }
//     console.log(dp)
//     return dp[prices.length-1][0]
// }
// module.exports = {
//     maxProfit : maxProfit
// };

//   *
//   * @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
};
全部评论

相关推荐

不愿透露姓名的神秘牛友
2024-12-30 18:02
程序员牛肉:1.可以标记一下自己的学校是985,有一些hr可能没想到你这个院校是985的。 2.简历所呈现出来的能力还是有点差的,苍穹外卖+黑马点评。这在java技术域里面也就是刚学三四个月的样子,大厂现在招人少,小厂又更加希望你能直接过来干活。就你简历上呈现出来的能力,确实是有点难找,肉眼可见的不懂技术。 第一个项目中:简单的使用redis也算是亮点嘛?使用jwt,threadlocal也算是亮点?你不就是调了几个包嘛?Nginx作为服务器也能写出来,这不是前端的活嘛? 第二个项目中:分布式锁+mq消息队列+Lua队列。真没啥好问的。属于面试官看一眼就阳痿的简历,没有任何想提问的欲望。 我给你建议是好好的挖一挖这个项目吧,其实苍穹外卖和黑马点评这两个项目很不错了,只不过是太烂大街了导致面试官没啥问的兴趣,所以不太推荐写简历上。
点赞 评论 收藏
分享
01-07 07:54
已编辑
门头沟学院 前端工程师
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务