{"id":529,"date":"2020-09-26T10:52:31","date_gmt":"2020-09-26T02:52:31","guid":{"rendered":"http:\/\/39.96.58.60\/?p=529"},"modified":"2022-10-18T16:38:55","modified_gmt":"2022-10-18T08:38:55","slug":"leetcode-152-%e4%b9%98%e7%a7%af%e6%9c%80%e5%a4%a7%e5%ad%90%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/www.yatenglg.cn\/blog\/?p=529","title":{"rendered":"Leetcode 152. \u4e58\u79ef\u6700\u5927\u5b50\u6570\u7ec4"},"content":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 <code>nums<\/code>&nbsp;\uff0c\u8bf7\u4f60\u627e\u51fa\u6570\u7ec4\u4e2d\u4e58\u79ef\u6700\u5927\u7684\u8fde\u7eed\u5b50\u6570\u7ec4\uff08\u8be5\u5b50\u6570\u7ec4\u4e2d\u81f3\u5c11\u5305\u542b\u4e00\u4e2a\u6570\u5b57\uff09\uff0c\u5e76\u8fd4\u56de\u8be5\u5b50\u6570\u7ec4\u6240\u5bf9\u5e94\u7684\u4e58\u79ef\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> [2,3,-2,4]\n\n<strong>\u8f93\u51fa:<\/strong> <code>6<\/code>\n\n<strong>\u89e3\u91ca:<\/strong>&nbsp;\u5b50\u6570\u7ec4 [2,3] \u6709\u6700\u5927\u4e58\u79ef 6\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> [-2,0,-1]\n\n<strong>\u8f93\u51fa:<\/strong> 0\n\n<strong>\u89e3\u91ca:<\/strong>&nbsp;\u7ed3\u679c\u4e0d\u80fd\u4e3a 2, \u56e0\u4e3a [-2,-1] \u4e0d\u662f\u5b50\u6570\u7ec4\u3002<\/pre>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6570\u7ec4\u3001 \u52a8\u6001\u89c4\u5212\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a52 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8668.90% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a16 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e869.44% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u6c42\u5b50\u533a\u95f4\u6700\u5927\u4e58\u79ef\uff0c\u6700\u5927\u4e58\u79ef\u53ea\u4e0e \u524d\u4e00\u4e2a\u76f8\u5173\n    \u4f46\u7531\u4e8e\u5b58\u5728\u6b63\u8d1f\uff0c\u6240\u4ee5\u5728\u5224\u65ad\u65f6\uff0c\u4e5f\u8bb0\u5f55\u6700\u5c0f\u503c\u3002\n\"\"\"\n\nclass Solution:\n    def maxProduct(self, nums: List[int]) -&gt; int:\n        n = len(nums)\n        dp_min = [[]for _ in range(n)]\n        dp_max = [[]for _ in range(n)]\n\n        dp_min[0] = nums[0]\n        dp_max[0] = nums[0]\n\n        for i in range(1, n):\n            dp_max[i] = max(dp_max[i-1]*nums[i], dp_min[i-1]*nums[i], nums[i])\n            dp_min[i] = min(dp_max[i-1]*nums[i], dp_min[i-1]*nums[i], nums[i])\n\n        return max(dp_max)\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a44 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8693.58% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.9 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8651.68% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u4f18\u5316\u4e86\u4e00\u4e0b\n\"\"\"\nclass Solution:\n    def maxProduct(self, nums: List[int]) -&gt; int:\n        n = len(nums)\n        pre_min = nums[0]\n        pre_max = nums[0]\n        max_ = pre_max\n        for i in range(1, n):\n            p_max = max(pre_max*nums[i], pre_min*nums[i], nums[i])\n            p_min = min(pre_max*nums[i], pre_min*nums[i], nums[i])\n            pre_max, pre_min = p_max, p_min\n            max_ = max(max_, pre_max)\n        return max_<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums&nbsp;\uff0c\u8bf7\u4f60\u627e\u51fa\u6570\u7ec4\u4e2d\u4e58\u79ef\u6700\u5927\u7684\u8fde\u7eed\u5b50\u6570\u7ec4\uff08\u8be5\u5b50\u6570\u7ec4\u4e2d\u81f3\u5c11\u5305\u542b\u4e00\u4e2a\u6570\u5b57\uff09\uff0c\u5e76\u8fd4&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/529"}],"collection":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=529"}],"version-history":[{"count":1,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/529\/revisions"}],"predecessor-version":[{"id":530,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/529\/revisions\/530"}],"wp:attachment":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}