{"id":5534,"date":"2019-09-07T21:23:10","date_gmt":"2019-09-08T04:23:10","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5534"},"modified":"2019-09-07T21:46:10","modified_gmt":"2019-09-08T04:46:10","slug":"leetcode-1186-maximum-subarray-sum-with-one-deletion","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1186-maximum-subarray-sum-with-one-deletion\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1186. Maximum Subarray Sum with One Deletion"},"content":{"rendered":"\n<p>Given an array of integers, return the maximum sum for a&nbsp;<strong>non-empty<\/strong>&nbsp;subarray (contiguous elements) with at most one element deletion.&nbsp;In other words, you want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the&nbsp;sum of the remaining elements is maximum possible.<\/p>\n\n\n\n<p>Note that the subarray needs to be&nbsp;<strong>non-empty<\/strong>&nbsp;after deleting one element.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [1,-2,0,3]\n<strong>Output:<\/strong> 4\n<strong>Explanation: <\/strong>Because we can choose [1, -2, 0, 3] and drop -2, thus the subarray [1, 0, 3] becomes the maximum value.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [1,-2,-2,3]\n<strong>Output:<\/strong> 3\n<strong>Explanation: <\/strong>We just choose [3] and it's the maximum sum.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [-1,-1,-1,-1]\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong>&nbsp;The final subarray needs to be non-empty. You can't choose [-1] and delete -1 from it, then get an empty subarray to make the sum equals to 0.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= arr.length &lt;= 10^5<\/code><\/li><li><code>-10^4 &lt;= arr[i] &lt;= 10^4<\/code><\/li><\/ul>\n\n\n\n<p><strong>Solution: DP<\/strong><\/p>\n\n\n\n<p>First, handle the special case: all numbers are negative, return the max one.<\/p>\n\n\n\n<p>s0 = max subarray sum ends with a[i]<br>s1 = max subarray sum ends with a[i] with at most one deletion<\/p>\n\n\n\n<p>whenever s0 or s1 becomes negative, reset them to 0.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maximumSum(vector<int>& arr) {    \n    int m = *max_element(begin(arr), end(arr));\n    if (m <= 0) return m;\n    \n    int s0 = 0;\n    int s1 = 0;\n    int ans = 0;\n    \n    for (int a : arr) {\n      s1 = max(s0, s1 + a);\n      s0 += a;\n      ans = max(ans, max(s0, s1));\n      if (s0 < 0) s0 = 0;\n      if (s1 < 0) s1 = 0;\n    }\n    \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers, return the maximum sum for a&nbsp;non-empty&nbsp;subarray (contiguous elements) with at most one element deletion.&nbsp;In other words, you want to choose&#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],"tags":[18,177,41,62],"class_list":["post-5534","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-medium","tag-subarray","tag-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5534","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=5534"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5534\/revisions"}],"predecessor-version":[{"id":5542,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5534\/revisions\/5542"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}