{"id":1888,"date":"2016-03-19T17:00:43","date_gmt":"2016-03-19T09:00:43","guid":{"rendered":"http:\/\/boweihe.me\/?p=1888"},"modified":"2016-03-19T17:00:43","modified_gmt":"2016-03-19T09:00:43","slug":"leetcode-53-maximum-subarray","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1888","title":{"rendered":"LeetCode 53. Maximum Subarray"},"content":{"rendered":"<p>Find the contiguous subarray within an array (containing at least one number) which has the largest sum.<br \/>\nFor example, given the array <code>[\u22122,1,\u22123,4,\u22121,2,1,\u22125,4]<\/code>,<br \/>\nthe contiguous subarray <code>[4,\u22121,2,1]<\/code> has the largest sum = <code>6<\/code>.<\/p>\n<h3>\u601d\u8def<\/h3>\n<p>\u8fd9\u4e2a\u662f\u5251\u6307Offer\u8fd8\u662f\u4ec0\u4e48\u7a0b\u5e8f\u5458\u9762\u8bd5\u7ecf\u5178\u6216\u8005\u662f\u7f16\u7a0b\u73e0\u7391\u4e0a\u7684\u4e00\u9053\u9898\uff0c\u53cd\u6b63\u5df2\u7ecf\u5e7f\u4e3a\u6d41\u4f20\u4e86\u3002<br \/>\nO(n)\u590d\u6742\u5ea6\u7684\u60f3\u6cd5\u662f\uff0c\u5047\u5b9a\u6211\u6709\u4e4b\u524d\u80fd\u5f97\u5230\u7684\u6700\u5927\u548c\u5b50\u4e32\u7684\u548csum[i-1]\u548c\u5f53\u524d\u503ca[i]\uff0c\u5982\u4f55\u5f97\u5230\u5f53\u524d\u7684\u6700\u5927\u548csum[i]\u5462\uff1f\u5206\u4e24\u4e2a\u60c5\u51b5\uff1a<\/p>\n<ul>\n<li>\u5982\u679csum[i-1]+a[i]&lt;a[i]\uff0c\u6bd4\u5982\u4e0a\u9762\u90a3\u4e2a\u6570\u5217\u7684\u524d\u4e24\u9879-2+1 = -1 &lt; 1\uff0c\u90a3\u610f\u601d\u5c31\u662f\u524d\u9762\u90a3\u4e48\u597d\u7684\u52a0\u8d77\u6765\u8fd8\u4e0d\u5982\u4ece\u6211\u8fd9\u8fb9\u91cd\u65b0\u5f00\u59cb\u6765\u7684\u597d\uff0c\u5219sum[1]\u5c31\u662fa[i]<\/li>\n<li>\u5426\u5219\uff0c\u66f4\u65b0sum[i]=sum[i-1]+a[i]<\/li>\n<\/ul>\n<p>\u6982\u62ec\u6210\u4e00\u53e5\u8bdd\u5c31\u662f\uff0c\u65e2\u7136\u4e4b\u524d\u7684\u4eba\u52a0\u5728\u4e00\u8d77\u90fd\u4e0d\u5982\u6211\uff0c\u90a3\u5c31\u6211\u5e26\u5934\u4e0a\u5427\uff01<\/p>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    int maxSubArray(vector&lt;int&gt;&amp; nums) {\n        if(nums.empty())\n            return -1;\n        int max = nums[0];\n        for(int i = 1; i&lt;nums.size(); i++){\n            if(nums[i-1] + nums[i] &gt; nums[i]){\n                nums[i] += nums[i-1];\n            }\n            if(nums[i] &gt; max)\n                max = nums[i];\n        }\n        return max;\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [\u22122,1,\u22123,4,\u22121,2,1,\u22125,4], the contiguous subarray [4,\u22121,2,1] has the largest sum = 6. \u601d\u8def \u8fd9\u4e2a\u662f\u5251\u6307Offer\u8fd8\u662f\u4ec0\u4e48\u7a0b\u5e8f\u5458\u9762\u8bd5\u7ecf\u5178\u6216\u8005\u662f\u7f16\u7a0b\u73e0\u7391\u4e0a\u7684\u4e00\u9053\u9898\uff0c\u53cd\u6b63\u5df2\u7ecf\u5e7f\u4e3a\u6d41\u4f20\u4e86\u3002 O(n)\u590d\u6742\u5ea6\u7684\u60f3\u6cd5\u662f\uff0c\u5047\u5b9a\u6211\u6709\u4e4b\u524d\u80fd\u5f97\u5230\u7684\u6700\u5927\u548c\u5b50\u4e32\u7684\u548csum[i-1]\u548c\u5f53\u524d\u503ca[i]\uff0c\u5982\u4f55\u5f97\u5230\u5f53\u524d\u7684\u6700\u5927\u548csum[i]\u5462\uff1f\u5206\u4e24\u4e2a\u60c5\u51b5\uff1a \u5982\u679csum[i-1]+a[i]&lt;a[i]\uff0c\u6bd4\u5982\u4e0a\u9762\u90a3\u4e2a\u6570\u5217\u7684\u524d\u4e24\u9879-2+1 = -1 &lt; 1\uff0c\u90a3\u610f\u601d\u5c31\u662f\u524d\u9762\u90a3\u4e48\u597d\u7684\u52a0\u8d77\u6765\u8fd8\u4e0d\u5982\u4ece\u6211\u8fd9\u8fb9\u91cd\u65b0\u5f00\u59cb\u6765\u7684\u597d\uff0c\u5219sum[1]\u5c31\u662fa[i] \u5426\u5219\uff0c\u66f4\u65b0sum[i]=sum[i-1]+a[i] \u6982\u62ec\u6210\u4e00\u53e5\u8bdd\u5c31\u662f\uff0c\u65e2\u7136\u4e4b\u524d\u7684\u4eba\u52a0\u5728\u4e00\u8d77\u90fd\u4e0d\u5982\u6211\uff0c\u90a3\u5c31\u6211\u5e26\u5934\u4e0a\u5427\uff01 \u4ee3\u7801 class Solution { public: int maxSubArray(vector&lt;int&gt;&amp; nums) { if(nums.empty()) return -1; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66],"class_list":["post-1888","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1888","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1888"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1888\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}