核心思路:动态规划
思路:
DP式:第i天的最大收益=MAX{前i-1天的最大收益,第i天价格-前i-1天的最低价格}
class Solution {
public int maxProfit(int[] prices) {
if(prices.length < 2){
return 0;
}
int max = 0;
int min = prices[0];
for(int i = 1; i < prices.length; i++){
max = Math.max(max,prices[i]-min);
min = Math.min(min,prices[i]);
}
return max;
}
}