範例2:
6(n) 10(D) int price[] = {30,20,45,38,10,20};
int win = 0;
int own = price[0];
int sell = 0;
for(int i=1;i<n;i++){
if(price[i]-own>=D){
sell = price[i];
win += sell - own;
own = Integer.MAX_VALUE; // 脫手賣掉=不持有
}
if(sell!=0){
if(sell-price[i]>=D){
own = price[i]; // 買進持有(Buy & Hold)
sell = 0;
}
}
}
System.out.println(win);