返回介绍

solution / 1100-1199 / 1186.Maximum Subarray Sum with One Deletion / README_EN

发布于 2024-06-17 01:03:22 字数 5193 浏览 0 评论 0 收藏 0

1186. Maximum Subarray Sum with One Deletion

中文文档

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

Solution 1: Preprocessing + Enumeration

We can first preprocess the array $arr$ to find the maximum subarray sum ending at and starting from each element, and store them in the arrays $left$ and $right$ respectively.

If we do not delete any element, then the maximum subarray sum is the maximum value in $left[i]$ or $right[i]$. If we delete one element, we can enumerate each position $i$ in $[1..n-2]$, calculate the value of $left[i-1] + right[i+1]$, and take the maximum value.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $arr$.

class Solution:
  def maximumSum(self, arr: List[int]) -> int:
    n = len(arr)
    left = [0] * n
    right = [0] * n
    s = 0
    for i, x in enumerate(arr):
      s = max(s, 0) + x
      left[i] = s
    s = 0
    for i in range(n - 1, -1, -1):
      s = max(s, 0) + arr[i]
      right[i] = s
    ans = max(left)
    for i in range(1, n - 1):
      ans = max(ans, left[i - 1] + right[i + 1])
    return ans
class Solution {
  public int maximumSum(int[] arr) {
    int n = arr.length;
    int[] left = new int[n];
    int[] right = new int[n];
    int ans = -(1 << 30);
    for (int i = 0, s = 0; i < n; ++i) {
      s = Math.max(s, 0) + arr[i];
      left[i] = s;
      ans = Math.max(ans, left[i]);
    }
    for (int i = n - 1, s = 0; i >= 0; --i) {
      s = Math.max(s, 0) + arr[i];
      right[i] = s;
    }
    for (int i = 1; i < n - 1; ++i) {
      ans = Math.max(ans, left[i - 1] + right[i + 1]);
    }
    return ans;
  }
}
class Solution {
public:
  int maximumSum(vector<int>& arr) {
    int n = arr.size();
    int left[n];
    int right[n];
    for (int i = 0, s = 0; i < n; ++i) {
      s = max(s, 0) + arr[i];
      left[i] = s;
    }
    for (int i = n - 1, s = 0; ~i; --i) {
      s = max(s, 0) + arr[i];
      right[i] = s;
    }
    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;
  }
};
func maximumSum(arr []int) int {
  n := len(arr)
  left := make([]int, n)
  right := make([]int, n)
  for i, s := 0, 0; i < n; i++ {
    s = max(s, 0) + arr[i]
    left[i] = s
  }
  for i, s := n-1, 0; i >= 0; i-- {
    s = max(s, 0) + arr[i]
    right[i] = s
  }
  ans := slices.Max(left)
  for i := 1; i < n-1; i++ {
    ans = max(ans, left[i-1]+right[i+1])
  }
  return ans
}
function maximumSum(arr: number[]): number {
  const n = arr.length;
  const left: number[] = Array(n).fill(0);
  const right: number[] = Array(n).fill(0);
  for (let i = 0, s = 0; i < n; ++i) {
    s = Math.max(s, 0) + arr[i];
    left[i] = s;
  }
  for (let i = n - 1, s = 0; i >= 0; --i) {
    s = Math.max(s, 0) + arr[i];
    right[i] = s;
  }
  let ans = Math.max(...left);
  for (let i = 1; i < n - 1; ++i) {
    ans = Math.max(ans, left[i - 1] + right[i + 1]);
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文