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

import java.util.*;


public class Solution {
    /**
     * 
     * @param prices int整型一维数组 
     * @return int整型
     */
    public int maxProfit (int[] prices) {
        // write code here
        if(prices == null ||prices.length == 1){
            return 0 ;
        }
        int buy = prices[0];
        int max = Integer.MIN_VALUE;
       //买小卖大
        for(int i = 1; i< prices.length; i++){
            //以最小的价格购买
            buy = Math.min(prices[i], buy);
            //当前价格 -  购买价格
            max = Math.max(max, prices[i] - buy);
            
        }
        return max;
    }
}
全部评论

相关推荐

小肥罗:哈哈哈哈hr:你来了,我先走了,后续概不负责
点赞 评论 收藏
分享
10-15 20:01
已编辑
上海大学 Java
钉钉什么垃圾公司,约面鸽人
光年在眼前:不是坏事,感觉钉钉挺逆天的,二面结束还给我留作业,让我使用钉钉和看最新的发布会,然后说感受,我是应该不会去,三面直接拒绝不面了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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