题解 | #买卖股票的最好时机(一)#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;
    }
}
全部评论

相关推荐

WhiteAlbum...:学院本2中大厂垂直实习➕acm比赛 秋招0面试
点赞 评论 收藏
分享
09-18 20:41
百度_Java
要个offer怎么这...:哈哈哈哈哈哈,我也拿了0x10000000个offer,秋招温啦啦啦,好开心
我的秋招日记
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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