{"id":8957,"date":"2021-12-01T20:48:06","date_gmt":"2021-12-02T04:48:06","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8957"},"modified":"2021-12-02T19:03:09","modified_gmt":"2021-12-03T03:03:09","slug":"leetcode-ultimate-dp-study-plan-day-6","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-ultimate-dp-study-plan-day-6\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode Ultimate DP Study Plan Day 6"},"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\uff01Day6 | Maximum Product Subarray\u3010\u8ddf\u6211\u4e00\u8d77\u5199\u4ee3\u7801\u3011\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/gwZm6mIYDfk?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>152.&nbsp;Maximum Product Subarray<\/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) -> O(1)\nclass Solution:\n  def maxProduct(self, nums: List[int]) -> int:\n    \n    @cache\n    def dp(i: int) -> Tuple[int, int]:\n      \"\"\"min \/ max subarray product ends with nums[i].\"\"\"\n      if i == 0: return nums[i], nums[i]\n      low, hi = dp(i - 1)\n      if nums[i] < 0: low, hi = hi, low\n      return min(low * nums[i], nums[i]), max(hi * nums[i], nums[i])\n    \n    return max(dp(i)[1] for i in range(len(nums)))\n\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1567.\u00a0Maximum Length of Subarray With Positive Product<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/dp-day6-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/dp-day6-1.png\" alt=\"\" class=\"wp-image-8997\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/dp-day6-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/dp-day6-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/dp-day6-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\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) -> O(1)\nclass Solution:\n  def getMaxLen(self, nums: List[int]) -> int:\n    \n    @cache\n    def dp(i: int) -> Tuple[int, int]:\n      \"\"\"Max length of pos\/neg product ends with nums[i].\"\"\"\n      if i < 0 or nums[i] == 0: return 0, 0\n      p, n = dp(i - 1)\n      if nums[i] > 0:\n        return p + 1, n + 1 if n else 0\n      else: # nums[i] < 0\n        return n + 1 if n else 0, p + 1\n    \n    return max(dp(i)[0] for i in range(len(nums)))\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>152.&nbsp;Maximum Product Subarray # Author: Huahua # Time complexity: O(n) # Space complexity: O(n) -> O(1) class Solution: def maxProduct(self, nums: List[int]) -> int: @cache&#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],"class_list":["post-8957","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-coding-with-me","tag-dp","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8957","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=8957"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8957\/revisions"}],"predecessor-version":[{"id":8999,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8957\/revisions\/8999"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}