leetcode 121. 买卖股票的最佳时机
思路:从后向前遍历,将数据分为不同的区间,计算区间的差值
class Solution {
public int maxProfit(int[] prices) {
int high;
int profit=0;
high=prices[prices.length-1];
for(int i=prices.length-2;i>=0;i--){
if(prices[i]>high){
high=prices[i];
continue;
}
else{
if(high-prices[i]>profit){
profit=high-prices[i];
}
}
}
return profit;
}
}