{"id":112,"date":"2017-08-01T20:22:15","date_gmt":"2017-08-01T12:22:15","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=112"},"modified":"2018-11-27T16:13:14","modified_gmt":"2018-11-27T08:13:14","slug":"53-maximum-subarray","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=112","title":{"rendered":"53. Maximum Subarray"},"content":{"rendered":"<p>Given an integer array\u00a0<code>nums<\/code>, find the contiguous subarray\u00a0(containing at least one number) which has the largest sum and return its sum.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre><strong>Input:<\/strong> [-2,1,-3,4,-1,2,1,-5,4],\r\n<strong>Output:<\/strong> 6\r\n<strong>Explanation:<\/strong>\u00a0[4,-1,2,1] has the largest sum = 6.\r\n<\/pre>\n<p><strong>Follow up:<\/strong><\/p>\n<p>If you have figured out the O(<em>n<\/em>) solution, try coding another solution using the divide and conquer approach, which is more subtle.<\/p>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass Solution(object):\r\n    def maxSubArray(self, nums):\r\n        &quot;&quot;&quot;\r\n        :type nums: List[int]\r\n        :rtype: int\r\n        &quot;&quot;&quot;\r\n        max_ending_here = max_so_far = nums[0]\r\n        for x in nums[1:]:\r\n            max_ending_here = max(x, max_ending_here + x)\r\n            max_so_far = max(max_so_far, max_ending_here)\r\n        return max_so_far\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array\u00a0nums, find the contiguous subarr<a href=\"https:\/\/www.ccagml.com\/?p=112\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">53. Maximum Subarray<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/112"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=112"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/112\/revisions"}],"predecessor-version":[{"id":113,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/112\/revisions\/113"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}