```
public int maxProfit(int[] prices) {
int length = prices.length;
if (length<=1) {
return 0;
}
int nPrevSell = 0;
int nPrevBuy = 0;
int nPrevCoolDown = 0;
int nSell = 0;
int nBuy =0;
int nCoolDown =0;
nSell = Integer.MIN_VALUE;
nBuy = -prices[0];
nCoolDown = 0;
for(int i=1;i<length;i++){
nPrevSell = nSell;
nPrevBuy = nBuy;
nPrevCoolDown =nCoolDown;
nSell = nPrevBuy+prices[i];
nCoolDown = Math.max(nPrevCoolDown, nPrevSell);
nBuy = Math.max(nPrevBuy, nPrevCoolDown-prices[i]);
}
return Math.max(nSell,nCoolDown);
}
```