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

相关推荐

积极的小学生不要香菜:你才沟通多少,没500不要说难
点赞 评论 收藏
分享
大飞的诡术妖姬:之前看b站多明海有个说法,日本就业竞争非常低的原因不光是毕业学生少,还有很多人干两年不喜欢职场氛围就辞职躺平,位置也空了很多,论吃苦耐劳还得看咱们
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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