Skip to content

Latest commit

 

History

History
161 lines (134 loc) · 3.9 KB

File metadata and controls

161 lines (134 loc) · 3.9 KB

中文文档

Description

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 <= 105
  • -104 <= arr[i] <= 104

Solutions

Python3

class Solution:
    def maximumSum(self, arr: List[int]) -> int:
        n = len(arr)
        left = [0] * n
        right = [0] * n
        t = 0
        for i, v in enumerate(arr):
            t = max(t, 0) + v
            left[i] = t
        t = 0
        for i in range(n - 1, -1, -1):
            t = max(t, 0) + arr[i]
            right[i] = t
        ans = max(left)
        for i in range(1, n - 1):
            ans = max(ans, left[i - 1] + right[i + 1])
        return ans

Java

class Solution {
    public int maximumSum(int[] arr) {
        int n = arr.length;
        int[] left = new int[n];
        int[] right = new int[n];
        int t = 0;
        for (int i = 0; i < n; ++i) {
            t = Math.max(t, 0) + arr[i];
            left[i] = t;
        }
        t = 0;
        for (int i = n - 1; i >= 0; --i) {
            t = Math.max(t, 0) + arr[i];
            right[i] = t;
        }
        int ans = Arrays.stream(left).max().getAsInt();
        for (int i = 1; i < n - 1; ++i) {
            ans = Math.max(ans, left[i - 1] + right[i + 1]);
        }
        return ans;
    }
}

C++

class Solution {
public:
    int maximumSum(vector<int>& arr) {
        int n = arr.size();
        int left[n];
        int right[n];
        for (int i = 0, t = 0; i < n; ++i) {
            t = max(t, 0) + arr[i];
            left[i] = t;
        }
        for (int i = n - 1, t = 0; ~i; --i) {
            t = max(t, 0) + arr[i];
            right[i] = t;
        }
        int ans = *max_element(left, left + n);
        for (int i = 1; i < n - 1; ++i) {
            ans = max(ans, left[i - 1] + right[i + 1]);
        }
        return ans;
    }
};

Go

func maximumSum(arr []int) int {
	n := len(arr)
	left := make([]int, n)
	right := make([]int, n)
	t := 0
	ans := math.MinInt32
	for i, v := range arr {
		t = max(t, 0) + v
		left[i] = t
		ans = max(ans, left[i])
	}
	t = 0
	for i := n - 1; i >= 0; i-- {
		t = max(t, 0) + arr[i]
		right[i] = t
	}
	for i := 1; i < n-1; i++ {
		ans = max(ans, left[i-1]+right[i+1])
	}
	return ans
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

...