https://www.lintcode.com/problem/best-time-to-buy-and-sell-stock-with-cooldown/description
public class Solution {
/**
* @param prices: a list of integers
* @return: return a integer
*/
public int maxProfit(int[] prices) {
// write your code here
int[] buy = new int[prices.length];
// 当天持有股票
int[] sell = new int[prices.length];
// 当天没有股票
buy[0] = -prices[0];
sell[0] = 0;
buy[1] = Math.max(buy[0], -prices[1]);
sell[1] = Math.max(sell[0], buy[0] + prices[1]);
for (int i = 2; i < prices.length; i++) {
int price = prices[i];
buy[i] = Math.max(buy[i - 1], sell[i - 2] - price);
sell[i] = Math.max(sell[i - 1], buy[i - 1] + price);
}
return sell[sell.length - 1];
}
}
网友评论