给定一个由N 个整数组成的数组arr[] ,任务是从给定数组中最多删除一个子数组后,找到任何可能的子数组的最大和。
例子:
Input: arr[] = {-1, 5, 2, -1, 6}
Output: 13
Explanation:
The maximum sum can be obtained by selecting the subarray {5, 2, -1, 6} and removing the subarray {-1}.
Input: arr[] = {7, 6, -1, -4, -5, 7, 1, -2, -3}
Output: 21
处理方法:按照以下步骤解决问题:
- 初始化一个数组,比如preSum[],这样preSum[i]存储范围[0, i]内任何子数组的最大子数组和。
- 初始化一个数组,比如suffSum[] ,使得suffSum[i]存储范围[i, N – 1]内任何子数组的最大子数组和。
- 初始化一个变量,比如在从给定数组中删除任何子数组后存储最大子数组总和的ans 。
- 从头开始遍历给定数组并使用 Kadane 算法更新preSum[] 。
- 从末尾遍历给定数组并使用 Kadane 算法更新postSum[] 。
- 迭代范围[0, N – 1]并更新ans的值ans和(preSum[i] + postSum[i + 1])的最大值。
- 完成上述步骤后,打印ans的值作为结果的最大和。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find maximum
// subarray sum possible after
// removing at most one subarray
void maximumSum(int arr[], int n)
{
int preSum[n];
int sum = 0;
int maxSum = 0;
// Calculate the preSum[] array
for(int i = 0; i < n; i++)
{
// Update the value of sum
sum = max(arr[i], sum + arr[i]);
// Update the value of maxSum
maxSum = max(maxSum, sum);
// Update the value of preSum[i]
preSum[i] = maxSum;
}
sum = 0;
maxSum = 0;
int postSum[n + 1];
// Calculate the postSum[] array
for(int i = n - 1; i >= 0; i--)
{
// Update the value of sum
sum = max(arr[i], sum + arr[i]);
// Update the value of maxSum
maxSum = max(maxSum, sum);
// Update the value of postSum[i]
postSum[i] = maxSum;
}
// Stores the resultant maximum
// sum of subarray
int ans = 0;
for(int i = 0; i < n; i++)
{
// Update the value of ans
ans = max(ans, preSum[i] + postSum[i]);
}
// Print the resultant sum
cout << (ans);
}
// Driver Code
int main()
{
int arr[] = { 7, 6, -1, -4, -5, 7, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
maximumSum(arr, n);
return 0;
}
// This code is contributed by ukasp
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to find maximum
// subarray sum possible after
// removing at most one subarray
public static void maximumSum(
int arr[], int n)
{
long[] preSum = new long[n];
long sum = 0;
long maxSum = 0;
// Calculate the preSum[] array
for (int i = 0; i < n; i++) {
// Update the value of sum
sum = Math.max(arr[i],
sum + arr[i]);
// Update the value of maxSum
maxSum = Math.max(maxSum,
sum);
// Update the value of preSum[i]
preSum[i] = maxSum;
}
sum = 0;
maxSum = 0;
long[] postSum = new long[n + 1];
// Calculate the postSum[] array
for (int i = n - 1; i >= 0; i--) {
// Update the value of sum
sum = Math.max(arr[i],
sum + arr[i]);
// Update the value of maxSum
maxSum = Math.max(maxSum,
sum);
// Update the value of postSum[i]
postSum[i] = maxSum;
}
// Stores the resultant maximum
// sum of subarray
long ans = 0;
for (int i = 0; i < n; i++) {
// Update the value of ans
ans = Math.max(ans,
preSum[i]
+ postSum[i + 1]);
}
// Print the resultant sum
System.out.println(ans);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 7, 6, -1, -4, -5, 7, 1 };
maximumSum(arr, arr.length);
}
}
Python3
# Python program for the above approach
# Function to find maximum
# subarray sum possible after
# removing at most one subarray
def maximumSum(arr, n) :
preSum = [0] * n
sum = 0
maxSum = 0
# Calculate the preSum[] array
for i in range(n):
# Update the value of sum
sum = max(arr[i], sum + arr[i])
# Update the value of maxSum
maxSum = max(maxSum,
sum)
# Update the value of preSum[i]
preSum[i] = maxSum
sum = 0
maxSum = 0
postSum = [0] * (n + 1)
# Calculate the postSum[] array
for i in range(n-1, -1, -1):
# Update the value of sum
sum = max(arr[i],
sum + arr[i])
# Update the value of maxSum
maxSum = max(maxSum,
sum)
# Update the value of postSum[i]
postSum[i] = maxSum
# Stores the resultant maximum
# sum of subarray
ans = 0
for i in range(n):
# Update the value of ans
ans = max(ans,
preSum[i]
+ postSum[i + 1])
# Print resultant sum
print(ans)
# Driver code
arr = [ 7, 6, -1, -4, -5, 7, 1]
N = len(arr)
maximumSum(arr, N)
# This code is contributed by sanjoy_62.
C#
// C# program for the above approach
using System;
class GFG{
// Function to find maximum
// subarray sum possible after
// removing at most one subarray
public static void maximumSum(int[] arr, int n)
{
long[] preSum = new long[n];
long sum = 0;
long maxSum = 0;
// Calculate the preSum[] array
for(int i = 0; i < n; i++)
{
// Update the value of sum
sum = Math.Max(arr[i], sum + arr[i]);
// Update the value of maxSum
maxSum = Math.Max(maxSum, sum);
// Update the value of preSum[i]
preSum[i] = maxSum;
}
sum = 0;
maxSum = 0;
long[] postSum = new long[n + 1];
// Calculate the postSum[] array
for(int i = n - 1; i >= 0; i--)
{
// Update the value of sum
sum = Math.Max(arr[i], sum + arr[i]);
// Update the value of maxSum
maxSum = Math.Max(maxSum, sum);
// Update the value of postSum[i]
postSum[i] = maxSum;
}
// Stores the resultant maximum
// sum of subarray
long ans = 0;
for(int i = 0; i < n; i++)
{
// Update the value of ans
ans = Math.Max(ans, preSum[i] +
postSum[i + 1]);
}
// Print the resultant sum
Console.WriteLine(ans);
}
// Driver code
static void Main()
{
int[] arr = { 7, 6, -1, -4, -5, 7, 1 };
maximumSum(arr, arr.Length);
}
}
// This code is contributed by abhinavjain194
Javascript
输出:
21
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live