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

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

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

对每天的价格: 保存一个之前的最小价格 保留一个之前的最大收益

import java.util.*;


public class Solution {
    public int maxProfit (int[] prices) {
        // write code here
        if(prices == null || prices.length == 0){
            return 0;
        }
        int len = prices.length;
        //每天与之前最小价格的差值
        int[] dp = new int[len];
        //最小价格
        int min = prices[0];
        //最大收益
        int max = 0;
        for(int i = 1; i < len; i++){
            dp[i] = prices[i] - min;
            min = Math.min(min,prices[i]);
            max = Math.max(max,dp[i]);
        }
        return max;
    }
}
全部评论

相关推荐

09-17 17:09
门头沟学院 Java
雨忄:有人给出过解法,拖晚点去,然后到时候再找其他理由商量,既增加他们的筛人成本,不一定会给你收回offer ,也能占位避免工贼
秋招的嫡长offer
点赞 评论 收藏
分享
09-29 16:59
已编辑
门头沟学院 Java
牛客96609213...:疯狂背刺,之前还明确设置截止日期,还有笔试,现在一帮人卡在复筛,他反而一边开启扩招,还给扩招的免笔试,真服了,你好歹先把复筛中的给处理了再说
投递大疆等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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