{"id":890,"date":"2025-10-23T19:11:58","date_gmt":"2025-10-23T11:11:58","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=890"},"modified":"2025-10-23T19:11:58","modified_gmt":"2025-10-23T11:11:58","slug":"leetcode-53-%e6%9c%80%e5%a4%a7%e5%ad%90%e6%95%b0%e7%bb%84%e5%92%8c","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=890","title":{"rendered":"LeetCode 53 \u2013 \u6700\u5927\u5b50\u6570\u7ec4\u548c"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums \uff0c\u8bf7\u4f60\u627e\u51fa\u4e00\u4e2a\u5177\u6709\u6700\u5927\u548c\u7684\u8fde\u7eed\u5b50\u6570\u7ec4\uff08\u5b50\u6570\u7ec4\u6700\u5c11\u5305\u542b\u4e00\u4e2a\u5143\u7d20\uff09\uff0c\u8fd4\u56de\u5176\u6700\u5927\u548c\u3002<\/p>\n<p>\u5b50\u6570\u7ec4\u662f\u6570\u7ec4\u4e2d\u7684\u4e00\u4e2a\u8fde\u7eed\u90e8\u5206\u3002<\/p>\n<p>\u793a\u4f8b 1\uff1a<\/p>\n<p>\u8f93\u5165\uff1anums = [-2,1,-3,4,-1,2,1,-5,4]<br \/>\n\u8f93\u51fa\uff1a6<br \/>\n\u89e3\u91ca\uff1a\u8fde\u7eed\u5b50\u6570\u7ec4 [4,-1,2,1] \u7684\u548c\u6700\u5927\uff0c\u4e3a 6 \u3002<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<h2>\u52a8\u6001\u89c4\u5212<\/h2>\n<p>\u52a8\u6001\u89c4\u5212\uff0c\u8bbe dp[i] \u8868\u793a\u4ee5 i \u4e3a\u7ed3\u5c3e\u7684\u5b50\u6570\u7ec4\u7684\u6700\u5927\u548c<br \/>\n\u5219\u6709 dp[i] = Math.max(dp[i &#8211; 1] + nums[i], nums[i])<br \/>\n\u8fdb\u4e00\u6b65\u4f1a\u53d1\u73b0 dp \u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u53ea\u4f7f\u7528\u4e00\u6b21\uff0c\u56e0\u6b64\u53ef\u4ee5\u7b80\u5316\u4e3a\u5355\u4e2a\u53d8\u91cf<br \/>\n\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(1)<\/p>\n<h2>\u9012\u5f52\u5206\u6cbb<\/h2>\n<p>\u628a\u6570\u7ec4\u4ece\u4e2d\u95f4\u4e00\u5206\u4e3a\u4e8c\uff1a\u5de6\u533a\u95f4 left\u3001\u53f3\u533a\u95f4 right<br \/>\n\u8bbe maxSum \u4e3a\u5b50\u533a\u95f4\u6700\u5927\u548c\uff0cmaxLSum \u4e3a\u5de6\u8fb9\u754c\u6700\u5927\u533a\u95f4\u548c\uff0cmaxRSum \u4e3a\u53f3\u8fb9\u754c\u6700\u5927\u533a\u95f4\u548c\uff0csum \u4e3a\u533a\u95f4\u548c\u3002<br \/>\n\u5219\u6709<br \/>\n&#8211; maxSum = max{left.maxSum\uff0cright.maxSum\uff0cleft.maxRSum + right.maxLSum}<br \/>\n&#8211; maxRSum = max{right.maxRSum, left.maxRSum + right.sum}<br \/>\n&#8211; maxLSum = max{left.maxLSum, left.sum + right.maxLSum}<\/p>\n<p>\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u9012\u5f52\u6765\u89e3\u51b3\uff0c\u9012\u5f52\u7684\u51fa\u53e3\u5c31\u662f\u533a\u95f4\u5185\u53ea\u5269\u4e00\u4e2a\u6570\u5b57\u3002<br \/>\n\u65f6\u95f4\u590d\u6742\u5ea6 O(n)<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public int maxSubArray1(int[] nums) {\n    int dp = nums[0];\n    int ans = nums[0];\n    for (int i = 1; i &lt; nums.length; i++) {\n        dp = Math.max(dp + nums[i], nums[i]);\n        ans = Math.max(dp, ans);\n    }\n    return ans;\n}\n\n\nprivate class Node {\n    int start, end;\n    int sum; \/\/ \u533a\u95f4\u548c\n    int maxSum, maxLSum, maxRSum;\/\/ \u5b50\u533a\u95f4\u6700\u5927\u548c\u3001\u5de6\u8fb9\u754c\u6700\u5927\u533a\u95f4\u548c\u3001\u53f3\u8fb9\u754c\u6700\u5927\u533a\u95f4\u548c\n\n    public Node(int start, int end) {\n        \/\/ \u524d\u95ed\u540e\u95ed\n        this.start = start;\n        this.end = end;\n    }\n\n    public void calculate(int[] nums) {\n        if (start == end) {\n            sum = nums[start];\n            maxSum = nums[start];\n            maxLSum = nums[start];\n            maxRSum = nums[start];\n            return;\n        }\n\n        int mid = (start + end) \/ 2;\n        Node left = new Node(start, mid);\n        Node right = new Node(mid + 1, end);\n        left.calculate(nums);\n        right.calculate(nums);\n\n        sum = left.sum + right.sum;\n\n        maxSum = Math.max(left.maxSum, right.maxSum);\n        maxSum = Math.max(maxSum, left.maxRSum + right.maxLSum);\n\n        maxLSum = Math.max(left.maxLSum, left.sum + right.maxLSum);\n        maxRSum = Math.max(right.maxRSum, right.sum + left.maxRSum);\n    }\n}\n\npublic int maxSubArray2(int[] nums) {\n    Node node = new Node(0, nums.length - 1);\n    node.calculate(nums);\n    return node.maxSum;\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun maxSubArray(nums: IntArray): Int {\n    var dp = nums[0]\n    var ans = nums[0]\n    for (i in 1 until nums.size) {\n        dp = max(nums[i], dp + nums[i])\n        ans = max(ans, dp)\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\u4e00\u4e2a\u5177\u6709\u6700\u5927\u548c\u7684\u8fde\u7eed\u5b50\u6570\u7ec4\uff08\u5b50\u6570\u7ec4\u6700\u5c11\u5305\u542b\u4e00\u4e2a\u5143\u7d20\uff09\uff0c\u8fd4\u56de\u5176\u6700 [&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,81,40,73,64],"class_list":["post-890","post","type-post","status-publish","format-standard","hentry","category-algo","tag-leetcode","tag-81","tag-40","tag-73","tag-64"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/890","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=890"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/890\/revisions"}],"predecessor-version":[{"id":891,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/890\/revisions\/891"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}