{"id":1517,"date":"2018-01-06T10:57:32","date_gmt":"2018-01-06T18:57:32","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1517"},"modified":"2018-01-08T08:57:06","modified_gmt":"2018-01-08T16:57:06","slug":"leetcode-309-best-time-to-buy-and-sell-stock-with-cooldown","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-309-best-time-to-buy-and-sell-stock-with-cooldown\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 309. Best Time to Buy and Sell Stock with Cooldown"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 309. Best Time to Buy and Sell Stock with Cooldown - \u5237\u9898\u627e\u5de5\u4f5c EP150\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/oL6mRyTn56M?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u6bcf\u5929\u7684\u80a1\u4ef7\uff0c\u6ca1\u6709\u4ea4\u6613\u6b21\u6570\u9650\u5236\uff0c\u4f46\u662f\u5356\u51fa\u540e\u8981\u4f11\u606f\u4e00\u5929\u624d\u80fd\u518d\u4e70\u8fdb\u3002\u95ee\u4f60\u6700\u5927\u6536\u76ca\u662f\u591a\u5c11\uff1f<\/p>\n<p>Say you have an array for which the\u00a0<i>i<\/i><sup>th<\/sup>\u00a0element is the price of a given stock on day\u00a0<i>i<\/i>.<\/p>\n<p>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:<\/p>\n<ul>\n<li>You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).<\/li>\n<li>After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)<\/li>\n<\/ul>\n<p><b>Example:<\/b><\/p>\n<pre class=\"\">prices = [1, 2, 3, 0, 2]\r\nmaxProfit = 3\r\ntransactions = [buy, sell, cooldown, buy, sell]<\/pre>\n<p><strong>Idea:<\/strong><\/p>\n<p>DP<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1522\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/309-ep150.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/309-ep150.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/309-ep150-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/309-ep150-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 12 ms\r\nclass Solution {\r\npublic:\r\n    int maxProfit(vector&lt;int&gt;&amp; prices) {\r\n        int sold = 0;\r\n        int rest = 0;\r\n        int hold = INT_MIN;\r\n        for (const int price : prices) {\r\n            int prev_sold = sold;\r\n            sold = hold + price;\r\n            hold = max(hold, rest - price);\r\n            rest = max(rest, prev_sold);\r\n        }\r\n        return max(rest, sold);\r\n    }\r\n};<\/pre>\n<p><strong>Related Problems:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-121-best-time-to-buy-and-sell-stock\/\">\u82b1\u82b1\u9171 LeetCode 121. Best Time to Buy and Sell Stock<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u6bcf\u5929\u7684\u80a1\u4ef7\uff0c\u6ca1\u6709\u4ea4\u6613\u6b21\u6570\u9650\u5236\uff0c\u4f46\u662f\u5356\u51fa\u540e\u8981\u4f11\u606f\u4e00\u5929\u624d\u80fd\u518d\u4e70\u8fdb\u3002\u95ee\u4f60\u6700\u5927\u6536\u76ca\u662f\u591a\u5c11\uff1f Say you have an array for which the\u00a0ith\u00a0element is the price of a given stock on day\u00a0i. Design an algorithm to find the maximum&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,205,206,107],"class_list":["post-1517","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-profit","tag-stock","tag-time","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=1517"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1517\/revisions"}],"predecessor-version":[{"id":1559,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1517\/revisions\/1559"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}