{"id":1039,"date":"2025-12-24T07:19:10","date_gmt":"2025-12-23T23:19:10","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=1039"},"modified":"2025-12-24T07:19:10","modified_gmt":"2025-12-23T23:19:10","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.guanhaobo.cn\/?p=1039","title":{"rendered":"LeetCode 152 \u2013 \u4e58\u79ef\u6700\u5927\u5b50\u6570\u7ec4"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums \uff0c\u8bf7\u4f60\u627e\u51fa\u6570\u7ec4\u4e2d\u4e58\u79ef\u6700\u5927\u7684\u975e\u7a7a\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<br \/>\n\u6d4b\u8bd5\u7528\u4f8b\u7684\u7b54\u6848\u662f\u4e00\u4e2a 32-\u4f4d \u6574\u6570\u3002<br \/>\n\u8bf7\u6ce8\u610f\uff0c\u4e00\u4e2a\u53ea\u5305\u542b\u4e00\u4e2a\u5143\u7d20\u7684\u6570\u7ec4\u7684\u4e58\u79ef\u662f\u8fd9\u4e2a\u5143\u7d20\u7684\u503c\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<br \/>\n\u8f93\u5165: nums = [2,3,-2,4]<br \/>\n\u8f93\u51fa: 6<br \/>\n\u89e3\u91ca: \u5b50\u6570\u7ec4 [2,3] \u6709\u6700\u5927\u4e58\u79ef 6\u3002<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>\u52a8\u6001\u89c4\u5212\u3002<br \/>\n\u5047\u8bbe dp[i] \u8868\u793a\u4ee5\u4f4d\u7f6e i \u7ed3\u5c3e\u7684\u6700\u5927\u4e58\u79ef\uff0c\u5219 <code>dp[i] = max(nums[i], dp[i-1] * nums[i])<\/code>\uff0c\u76f4\u63a5\u9012\u63a8\u5c31\u53ef\u4ee5\u4e86\u3002<br \/>\n\u4f46\u9898\u76ee\u4e2d\u662f\u6709\u8d1f\u6570\u7684\uff0c\u5b58\u5728\u8d1f\u8d1f\u5f97\u6b63\u7684\u95ee\u9898\uff0c\u800c\u4e0a\u9762\u7684 dp[i] \u5bf9\u8d1f\u6570\u662f\u4e0d\u517c\u5bb9\u7684\u3002\u5982\u679c\u4ece\u7b26\u53f7\u5165\u624b\uff0c\u6b63\u6570\u7ef4\u62a4\u4e00\u5957\u7ed3\u679c\uff0c\u8d1f\u6570\u7ef4\u62a4\u4e00\u5957\u7ed3\u679c\uff0c\u95ee\u9898\u4f1a\u53d8\u5f97\u590d\u6742\u3002<\/p>\n<p>\u53ef\u4ee5\u5f15\u5165\u6700\u5c0f\u4e58\u79ef\u6765\u89e3\u51b3\u8d1f\u6570\u95ee\u9898\uff0cmax[i] \u8868\u793a\u4ee5\u4f4d\u7f6e i \u7ed3\u5c3e\u7684\u6700\u5927\u4e58\u79ef\uff0cmin[i] \u8868\u793a\u6700\u5c0f\u4e58\u79ef\u3002\u5982\u679c\u6709\u8d1f\u6570\uff0c\u81ea\u7136\u4f1a\u88ab\u4fdd\u5b58\u5728 min[i] \u4e2d\uff0c\u4ee3\u7801\u4e2d\u65e0\u9700\u7279\u6b8a\u5904\u7406\u3002<br \/>\n\u9012\u63a8\u516c\u5f0f\u662f<code>max[i] = max(nums[i], max[i-1] * nums[i], min[i-1] * num[i])<\/code> \u548c <code>min[i] = min(nums[i], max[i-1] * nums[i], min[i-1] * num[i])<\/code> .<br \/>\n\u8fdb\u4e00\u6b65\uff0c\u5bf9\u4e8e max \u548c min \u8fd9\u4e24\u4e2a\u6570\u7ec4\uff0c\u4f1a\u53d1\u73b0\u6bcf\u6b21\u53ea\u7528\u5230\u4e86 i &#8211; 1\uff0c\u53ef\u4ee5\u4f7f\u7528\u53d8\u91cf\u4ee3\u66ff\uff0c\u8282\u7701\u7a7a\u95f4\u3002<\/p>\n<p>\u65f6\u95f4\u590d\u6742\u5ea6 O(n).<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public int maxProduct(int[] nums) {\n    int max = nums[0], min = nums[0];\n    int ans = nums[0];\n    for (int i = 1; i &lt; nums.length; i++) {\n        int lastMax = max, lastMin = min;\n        max = Math.max(nums[i], Math.max(lastMax * nums[i], lastMin * nums[i]));\n        min = Math.min(nums[i], Math.min(lastMax * nums[i], lastMin * nums[i]));\n        ans = Math.max(ans, Math.max(max, min));\n    }\n    return ans;\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun maxProduct(nums: IntArray): Int {\n    var max = nums[0]\n    var min = nums[0]\n    var ans = nums[0]\n    for (i in 1 until nums.size) {\n        val lastMax = max\n        val lastMin = min\n        max = nums[i]\n            .coerceAtLeast(lastMax * nums[i])\n            .coerceAtLeast(lastMin * nums[i])\n        min = nums[i]\n            .coerceAtMost(lastMax * nums[i])\n            .coerceAtMost(lastMin * nums[i])\n        ans = ans.coerceAtLeast(max).coerceAtLeast(min)\n    }\n    return ans\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums \uff0c\u8bf7\u4f60\u627e\u51fa\u6570\u7ec4\u4e2d\u4e58\u79ef\u6700\u5927\u7684\u975e\u7a7a\u8fde\u7eed \u5b50\u6570\u7ec4\uff08\u8be5\u5b50\u6570\u7ec4\u4e2d\u81f3\u5c11\u5305\u542b\u4e00\u4e2a\u6570\u5b57\uff09 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[20,40],"class_list":["post-1039","post","type-post","status-publish","format-standard","hentry","category-algo","tag-leetcode","tag-40"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1039","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1039"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1039\/revisions"}],"predecessor-version":[{"id":1040,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1039\/revisions\/1040"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}