实时更新最大值和最小值,然后返回最大值就好了
class Solution {
public int maxProfit(int[] prices) {
int len = prices.length;
int min = prices[0];
int max = Integer.MIN_VALUE;
for(int i = 0; i < len ; i++){
if(min > prices[i])
min = prices[i];
max = Math.max(max,prices[i] - min);
}
return max;
}
}
同 剑指 Offer 63. 股票的最大利润