题解 | #买卖股票的最好时机(一) 与连续子数组最大值解法一致#

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

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

import java.util.*;


public class Solution {
    /**
     * 
     * @param prices int整型一维数组 
     * @return int整型
     */
    public int maxProfit (int[] prices) {
        // write code here
        if(prices.length == 0){
            return 0;
        }
        int[] profit = new int[prices.length];
        int lowerPrice = prices[0];
        for(int i = 0; i < prices.length; i++){
            if(prices[i] > lowerPrice){
                profit[i] = prices[i] - lowerPrice;
            }
            else {
                profit[i] = 0;
                lowerPrice = prices[i];
            }
        }
        Arrays.sort(profit);
        return profit[profit.length - 1];
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
今天 14:00
点赞 评论 收藏
分享
风中翠竹:真的真的真的没有kpi。。。面试官是没有任何kpi的,捞是真的想试试看这个行不行,碰碰运气,或者是面试官比较闲现在,没事捞个人看看。kpi算HR那边,但是只有你入职了,kpi才作数,面试是没有的。
双非有机会进大厂吗
点赞 评论 收藏
分享
半解316:内容充实,细节需要修改一下。 1,整体压缩为一页。所有内容顶格。 2,项目描述删除,直接写个人工作量 修改完之后还需要建议,可以私聊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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