{"id":9073,"date":"2021-12-09T21:16:04","date_gmt":"2021-12-10T05:16:04","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9073"},"modified":"2021-12-09T21:30:10","modified_gmt":"2021-12-10T05:30:10","slug":"leetcode-ultimate-dp-study-plan-day-8","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-ultimate-dp-study-plan-day-8\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode Ultimate DP Study Plan Day 8"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"LeetCode DP\u7ec8\u6781\u5b66\u4e60\u8ba1\u5212\uff01Day8 | Best Time to Buy and Sell Stock with Cooldown\u3010\u8ddf\u6211\u4e00\u8d77\u5199\u4ee3\u7801\u3011\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/xXIs_bF8lHE?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>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>309.\u00a0Best Time to Buy and Sell Stock with Cooldown<\/strong><\/h2>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\n# Time complexity: O(n)\n# Space complexity: O(n)\nclass Solution:\n  def maxProfit(self, prices: List[int]) -> int:\n    \n    @cache\n    def dp(i: int) -> Tuple[int, int, int]:\n      \"\"\"\n      Returns\n      1) sold: Max profit w\/o holdings. Can't buy. Can't sell.\n      2) holding: Max profit w\/ holdings. Can't buy, Can sell.\n      3) cooldown: Max profit w\/o holdings. Can buy. Can't sell.\"\"\"\n      if i < 0: return -10**9, -10**9, 0\n      sold, holding, cooldown = dp(i - 1)\n      \n      return (holding + prices[i], # sold\n              max(holding, cooldown - prices[i]), # do nothing, or buy.\n              max(cooldown, sold)) # cooldown\n    \n    return max(dp(len(prices) - 1))\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>714 Best Time to Buy and Sell Stock with Transaction Fee<\/strong><\/h2>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\n# Time complexity: O(n)\n# Space complexity: O(n)\nclass Solution:\n  def maxProfit(self, prices: List[int], fee: int) -> int:\n    \n    @cache\n    def dp(i: int) -> Tuple[int, int]:\n      \"\"\"\n      Returns:\n      1) sold: Max profit w\/o holdings.\n      2) holding: Max profit w\/ holdings.\"\"\"\n      if i < 0: return 0, -10**9\n      sold, holding = dp(i - 1)\n      return (max(sold, holding + prices[i] - fee), # do nothing, sell\n              max(holding, sold - prices[i]))       # do nothing, buy\n    \n    return dp(len(prices) - 1)[0]\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>309.\u00a0Best Time to Buy and Sell Stock with Cooldown # Author: Huahua # Time complexity: O(n) # Space complexity: O(n) class Solution: def maxProfit(self, prices:&#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":[740,18,744],"class_list":["post-9073","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-coding-with-me","tag-dp","tag-stock-problems","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9073","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=9073"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9073\/revisions"}],"predecessor-version":[{"id":9076,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9073\/revisions\/9076"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}