分析:利润为负数统一为 0利润
prices = [7,1,5,3,6,4]
1.我们要顺着找出,最便宜的买点 和 最贵的卖点
2.然后 profit = 最贵的卖点 - 最便宜的买点
3.那么定义 maxProfit = 0 记录最贵的卖点,minPrice = prices[0]为最便宜的买点
4.然后顺着逐一比对寻找即可
int maxProfit = 0;//定义利润
int minPrice = Integer.MAX_VALUE;//定义买入的价格
//每一天的利润最大化 = 今天的价格 - 最小买入价格 或者 昨天的利润最大化
for (int price : prices) {
//一定是先买后卖!!!!
minPrice = Math.min(price, minPrice);//更新最小买入价格
maxProfit = Math.max(maxProfit, price - minPrice);//更新最大利润
}
return maxProfit;//返回最大利润