1暴力
public int maxProfit(int[] prices) {
if(prices.length<=1){
return 0;
}
int max =0;
for (int i = 0; i <prices.length ; i++) {
int temp = prices[i];
for (int j = i; j <prices.length ; j++) {
if(temp < prices[j]){
max=Math.max(max ,prices[j]-temp);
}
}
}
return max;
}
2 动态
public int maxProfit1(int[] prices) {
int min=0;
int minValue=0;
for (int i = 1; i <prices.length ; i++) {
if(prices[i]>=prices[min]){
if(prices[i]-prices[min]>minValue){
minValue=prices[i]-prices[min];
}
}else {
min=i;
}
}
return minValue;
}