Best Time to Buy and Sell Stock with Cooldown -- LeetCode

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
transactions = [buy, sell, cooldown, buy, sell]

思路:DP。

因为这个题中有两种操作buy 和 sell,这里我们用buy[i]表示在第i天或之前买入股票所能获得的最大利益(即最后一次交易为买入股票),用sell[i]表示在第i天或之前卖出股票所能获得的最大利益。我们分别考虑转化公式。

对于buy[i],我们有两种选择,一是这一天不买入股票,则最大利润为buy[i-1];或者这一天买入股票,因为买入股票的前一天不能卖出股票,则最大利润应该是两天之前卖出股票的最大利润减去今日的股价sell[i-2] - prices[i]。所以

buy[i] = max(buy[i-1], sell[i-2] - prices[i])

对于sell[i],我们也有两种选择,一是这一天不卖出股票,则最大利润为sell[i-1];或者这一天卖出股票,则为前一天为止买入股票所能获得的最大利润加上今日的股价buy[i-1] + prices[i]。所以

sell[i] = max(sell[i-1], buy[i-1] + prices[i])

完整程序:

 1 class Solution {
 2 public:
 3     int maxProfit(vector<int>& prices) {
 4         if (prices.size() == 0) return 0;
 5         vector<int> buy(prices.size());
 6         vector<int> sell(prices.size());
 7         sell[0] = 0;
 8         buy[0] = -prices[0];
 9         for (int i = 1; i < prices.size(); i++) {
10             buy[i] = std::max(buy[i-1], (i > 1 ? sell[i-2] : 0) - prices[i]);
11             sell[i] = std::max(sell[i-1], buy[i-1] + prices[i]);
12         }
13         return sell.back();
14     }
15 };

实际上,我们可以做到O(1)空间复杂度。

 1 class Solution {
 2 public:
 3     int maxProfit(vector<int>& prices) {
 4         if (prices.size() == 0) return 0;
 5         int prev_sell(0), prev_buy, sell(0), buy(INT_MIN);
 6         for (int i = 0, n = prices.size(); i < n; i++) {
 7             prev_buy = buy;
 8             buy = std::max(prev_buy, prev_sell - prices[i]);
 9             prev_sell = sell;
10             sell = std::max(prev_sell, prev_buy + prices[i]);
11         }
12         return sell;
13     }
14 };
时间: 2024-09-29 10:54:07

Best Time to Buy and Sell Stock with Cooldown -- LeetCode的相关文章

309. Best Time to Buy and Sell Stock with Cooldown

/* * 309. Best Time to Buy and Sell Stock with Cooldown * 2016-7-4 by Mingyang * http://buttercola.blogspot.com/2016/01/leetcode-best-time-to-buy-and-sell.html * *1. Define States * *To represent the decision at index i: *buy[i]: Max profit till inde

LeetCode Best Time to Buy and Sell Stock with Cooldown

原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ 题目: 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

Best Time to Buy and Sell Stock with Cooldown

题目: 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)

解题报告Best Time to Buy and Sell Stock with Cooldown

题目 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)

Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown)

股票问题: 309. 最佳买卖股票时机含冷冻期 714. 买卖股票的最佳时机含手续费 给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 .? 设计一个算法计算出最大利润.在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票): 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天). 示例: 输入: [1,2,3,0,2] 输出: 3 解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入,

Best Time to Buy and Sell Stock系列

1.Best Time to Buy and Sell Stock II 1 class Solution { 2 public: 3 int maxProfit(vector<int>& prices) { 4 if(prices.size() < 1) //prices.size is unsigned int 5 return 0; 6 int pro = 0; 7 for(int i=0; i<prices.size()-1; i++) 8 { 9 if(price

【leetcode】122.Best Time to Buy and Sell Stock II

@requires_authorization @author johnsondu @create_time 2015.7.19 21:01 @url [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) /************************ * @description: dynamic programming. * 相邻元素做

[LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

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 at most two transactions. Note:You may not engage in multiple transactions at the same time (ie,

LeetCode Best Time to Buy and Sell Stock II

Best Time to Buy and Sell Stock II Total Accepted: 41127 Total Submissions: 108434 My Submissions Question Solution 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