【leetcode】1186. Maximum Subarray Sum with One Deletion

Topics are as follows:

Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elements) with at most one element deletion. 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 sum of the remaining elements is maximum possible.

Note that the subarray needs to be non-empty after deleting one element.

 

Example 1:

Input: arr = [1,-2,0,3]
Output: 4
Explanation: Because we can choose [1, -2, 0, 3] and drop -2, thus the subarray [1, 0, 3] becomes the maximum value.

Example 2:

Input: arr = [1,-2,-2,3]
Output: 3
Explanation: We just choose [3] and it's the maximum sum.

Example 3:

Input: arr = [-1,-1,-1,-1]
Output: -1
Explanation: 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.

 

Constraints:

  • 1 <= arr.length <= 10^5
  • -10^4 <= arr[i] <= 10^4

: Solving ideas can be obtained and the maximum assumed after deleting the sub-arrays arr [i], then the sub-array and is equivalent arr [i] is divided into two parts, as long as such find x: i-1 > x> = 0, y: i + 1 <y <len (arr), such that the sum (arr [x: i- 1]) and the sum (arr [i + 1: y]) the maximum can be obtained. So how do find the x and y? And the minimum value x, for example, just start from index 0 and arr successively accumulated, there have been recorded, then the sum (arr [x: i- 1]) is the maximum value of sum (arr [0: i and the minimum value -1]) by subtracting appeared; Similarly, y is the same method of seeking. There are two special cases need to be considered separately, that is the largest sub-arrays only took part of the left or right of i, or is the whole array arr.

code show as below:

class Solution(object):
    def maximumSum(self, arr):
        """
        :type arr: List[int]
        :rtype: int
        """
        if len(arr) == 1:
            return arr[0]
        elif len(arr) == 2:
            return max(sum(arr), arr[0], arr[1])
        amount = arr[0] + arr[1]
        min_left = arr[0]
        left = [None, arr[0]]
        for i in range(2, len(arr)):
            left.append(max(amount, amount - min_left))
            min_left = min(amount, min_left)
            amount += arr[i]
        res = amount # set ret equals to the sum of the arr

        amount = arr[-1] + arr[-2]
        min_right = arr[-1]

        res = max(res, left[-1])  # if remove the last element
        res = max(res, left[len(arr) - 2], arr[-1], left[len(arr) - 2] + arr[-1]) # remove the second-last element

        for i in range(len(arr) - 3, -1, -1):
            right_val = max(amount, amount - min_right)
            min_right = min(amount, min_right)
            amount += arr[i]
            if left[i] == None:
                res = max(res, right_val)
            else:
                res = max(res, left[i], right_val, left[i] + right_val)
        return res

 

Guess you like

Origin www.cnblogs.com/seyjs/p/11511403.html