思路:是用贪心找到利润最大值。如果需要dp解法可以看股票类型详解:股票题型详解
class Solution {
public int maxProfit(int[] prices) {
if(prices.length == 0 || prices.length == 1 ) return 0;
int ans = 0 ;
int min = prices[0];
for(int i = 1 ; i < prices.length ; i++){
ans = Math.max(ans,prices[i] - min); // 计算利润最大值
min = Math.min(min,prices[i]); // 贪心 找到成本最小值
}
return ans;
}
}