package leetcode;
public class demo_309 {
public int maxProfit(int[] prices) {
//前i天中,最后一个操作是买入的最大收益
int[] buy=new int[prices.length];
//前i天中,最后一个操作是卖出的最大收益
int[] sell=new int[prices.length];
//前i天中,最后一个操作是冷冻期的最大收益
int[] cold=new int[prices.length];
buy[0]=-prices[0];
for (int i = 1; i < prices.length; i++) {
//第i天要么卖出要么不卖出
sell[i]=Math.max(buy[i-1]+prices[i], sell[i-1]);
//第i天要么买,要么不买
buy[i]=Math.max(cold[i-1]-prices[i],buy[i-1]);
//根据转换公式,冷冻期最大收益和卖出去是一样的
cold[i]=sell[i-1];
}
System.out.println(sell[prices.length-1]);
return sell[prices.length-1];
}
public static void main(String[] args) {
// TODO Auto-generated method stub
demo_309 demo=new demo_309();
int[] prices= {1,2,3,0,2};
demo.maxProfit(prices);
}
}