题目:股票买卖问题,{7, 1, 5, 3, 6, 4}。买卖2次
public static void main(String[] args){
int[] array={7, 1, 5, 3, 6, 4};
System.out.println(maxProfit(array));
}
public static int maxProfit(int[] array){
if(array==null ||array.length<1){
return 0;
}
int sell1=0;
int sell2=0;
int buy1=Integer.MIN_VALUE;
int buy2=Integer.MIN_VALUE;
for(int i=0;i<array.length;i++){
buy1=Math.max(buy1,0-array[i]);
sell1=Math.max(sell1, buy1+array[i]);
buy2=Math.max(buy2, sell1-array[i]);
sell2=Math.max(sell2, buy2+array[i]);
}
return sell2;
}