{"id":949,"date":"2017-11-27T21:07:38","date_gmt":"2017-11-28T05:07:38","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=949"},"modified":"2018-04-19T08:33:51","modified_gmt":"2018-04-19T15:33:51","slug":"leetcode-53-maximum-subarray","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-53-maximum-subarray\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 53. Maximum Subarray"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 53. Maximum Subarray - \u5237\u9898\u627e\u5de5\u4f5c EP25\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/7J5rs56JBs8?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><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Find the contiguous subarray within an array (containing at least one number) which has the largest sum.<\/p>\n<p>For example, given the array\u00a0<code>[-2,1,-3,4,-1,2,1,-5,4]<\/code>,<br \/>\nthe contiguous subarray\u00a0<code>[4,-1,2,1]<\/code>\u00a0has the largest sum =\u00a0<code>6<\/code>.<\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>DP<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 6 ms (better than 98.66%)\r\nclass Solution {\r\npublic:\r\n    int maxSubArray(vector&lt;int&gt;&amp; nums) {\r\n        vector&lt;int&gt; f(nums.size());\r\n        f[0] = nums[0];\r\n        \r\n        for (int i = 1; i &lt; nums.size(); ++i)\r\n            f[i] = max(f[i - 1] + nums[i], nums[i]);\r\n        \r\n        return *std::max_element(f.begin(), f.end());\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array\u00a0[-2,1,-3,4,-1,2,1,-5,4], the contiguous&#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,163],"tags":[18,141],"class_list":["post-949","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","category-easy","tag-dp","tag-max","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/949","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=949"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/949\/revisions"}],"predecessor-version":[{"id":2606,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/949\/revisions\/2606"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}