### 题目

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:

You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day) Example:

prices = [1, 2, 3, 0, 2]
maxProfit = 3

### 比较笨的动态规划

...
...

#### 代码

class Solution {
public int maxProfit(int[] prices) {
int[] dp = new int[prices.length+2];
for (int i = 3; i < dp.length; i++) {
int curr = prices[i-2];
if (curr <= prices[i-3]) {
dp[i] = dp[i-1];
} else {
int max = 0;
for (int j = 2; j <= i; j++) {
max = Math.max(max, dp[j-2] + curr - prices[j-2]);
}
dp[i] = max;
}
}
return dp[dp.length-1];
}
}

### 更好的动态规划

2. 以一个买入sell动作结束

sellToday = Max (sellYesterday, buyYesterday + priceToday)

#### 代码

class Solution {
public int maxProfit(int[] prices) {
if (prices.length == 0) { return 0; }