📌  相关文章
📜  删除至少一个元素后,最大化最大子数组总和

📅  最后修改于: 2021-04-23 17:12:05             🧑  作者: Mango

给定N个整数的数组arr [] 。任务是首先找到最大子阵列总和,然后从子阵列中移除最多一个元素。如果存在多个子数组,且子数组的总和最大,则最多移除单个元素,以使移除后的最大总和最大化。任务是使移除后获得的总和最大化。

注意:您必须首先找到最大子数组总和,然后在必要时从该子数组中删除该元素。同样在移除后,子阵列的大小至少应为1。

例子:

方法:使用Kadane算法查找最大子数组和。找到总和后,请重新应用Kadane的算法,以稍作更改再次找到最大和。在循环中使用两个额外的变量cntmini 。变量cnt对子数组中的元素数进行计数,并且mini将最小值存储在所有子数组中,这些子数组的和与最大子数组的总和相同。如果由此获得的最小元素小于0 ,那么只有我们从子数组中删除一个元素,否则我们就不这样做。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the maximum sub-array sum
int maxSubArraySum(int a[], int size)
{
  
    // Initialized
    int max_so_far = INT_MIN, max_ending_here = 0;
  
    // Traverse in the array
    for (int i = 0; i < size; i++) {
  
        // Increase the sum
        max_ending_here = max_ending_here + a[i];
  
        // If sub-array sum is more than the previous
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;
  
        // If sum is negative
        if (max_ending_here < 0)
            max_ending_here = 0;
    }
    return max_so_far;
}
  
// Function that returns the maximum sub-array sum
// after removing an element from the same sub-array
int maximizeSum(int a[], int n)
{
    int cnt = 0;
    int mini = INT_MAX;
    int minSubarray = INT_MAX;
  
    // Maximum sub-array sum using Kadane's Algorithm
    int sum = maxSubArraySum(a, n);
  
    int max_so_far = INT_MIN, max_ending_here = 0;
  
    // Re-apply Kadane's with minor changes
    for (int i = 0; i < n; i++) {
  
        // Increase the sum
        max_ending_here = max_ending_here + a[i];
        cnt++;
        minSubarray = min(a[i], minSubarray);
  
        // If sub-array sum is greater than the previous
        if (sum == max_ending_here) {
  
            // If elements are 0, no removal
            if (cnt == 1)
                mini = min(mini, 0);
  
            // If elements are more, then store
            // the minimum value in the sub-array
            // obtained till now
            else
                mini = min(mini, minSubarray);
        }
  
        // If sum is negative
        if (max_ending_here < 0) {
  
            // Re-initialize everything
            max_ending_here = 0;
            cnt = 0;
            minSubarray = INT_MAX;
        }
    }
  
    return sum - mini;
}
  
// Driver code
int main()
{
    int a[] = { 1, 2, 3, -2, 3 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << maximizeSum(a, n);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
      
// Function to return the maximum sub-array sum
static int maxSubArraySum(int a[], int size)
{
  
    // Initialized
    int max_so_far = Integer.MIN_VALUE, 
        max_ending_here = 0;
  
    // Traverse in the array
    for (int i = 0; i < size; i++)
    {
  
        // Increase the sum
        max_ending_here = max_ending_here + a[i];
  
        // If sub-array sum is more than the previous
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;
  
        // If sum is negative
        if (max_ending_here < 0)
            max_ending_here = 0;
    }
    return max_so_far;
}
  
// Function that returns the maximum sub-array sum
// after removing an element from the same sub-array
static int maximizeSum(int a[], int n)
{
    int cnt = 0;
    int mini = Integer.MAX_VALUE;
    int minSubarray = Integer.MAX_VALUE;
  
    // Maximum sub-array sum 
    // using Kadane's Algorithm
    int sum = maxSubArraySum(a, n);
  
    int max_so_far = Integer.MIN_VALUE, 
        max_ending_here = 0;
  
    // Re-apply Kadane's with minor changes
    for (int i = 0; i < n; i++)
    {
  
        // Increase the sum
        max_ending_here = max_ending_here + a[i];
        cnt++;
        minSubarray = Math.min(a[i], minSubarray);
  
        // If sub-array sum is greater than the previous
        if (sum == max_ending_here)
        {
  
            // If elements are 0, no removal
            if (cnt == 1)
                mini = Math.min(mini, 0);
  
            // If elements are more, then store
            // the minimum value in the sub-array
            // obtained till now
            else
                mini = Math.min(mini, minSubarray);
        }
  
        // If sum is negative
        if (max_ending_here < 0)
        {
  
            // Re-initialize everything
            max_ending_here = 0;
            cnt = 0;
            minSubarray = Integer.MAX_VALUE;
        }
    }
  
    return sum - mini;
}
  
// Driver code
public static void main(String[] args)
{
    int a[] = { 1, 2, 3, -2, 3 };
    int n = a.length;
    System.out.println(maximizeSum(a, n));
}
}
  
// This code is contributed by Code_Mech


Python3
# Python3 implementation of the approach 
import sys;
  
# Function to return the maximum sub-array sum 
def maxSubArraySum(a, size) : 
  
    # Initialized 
    max_so_far = -(sys.maxsize - 1);
    max_ending_here = 0; 
  
    # Traverse in the array 
    for i in range(size) :
  
        # Increase the sum 
        max_ending_here = max_ending_here + a[i]; 
  
        # If sub-array sum is more than the previous 
        if (max_so_far < max_ending_here) :
            max_so_far = max_ending_here; 
  
        # If sum is negative 
        if (max_ending_here < 0) :
            max_ending_here = 0; 
      
    return max_so_far; 
  
# Function that returns the maximum 
# sub-array sum after removing an 
# element from the same sub-array 
def maximizeSum(a, n) : 
  
    cnt = 0; 
    mini = sys.maxsize;
    minSubarray = sys.maxsize; 
  
    # Maximum sub-array sum using
    # Kadane's Algorithm 
    sum = maxSubArraySum(a, n); 
  
    max_so_far = -(sys.maxsize - 1);
    max_ending_here = 0; 
  
    # Re-apply Kadane's with minor changes 
    for i in range(n) :
  
        # Increase the sum 
        max_ending_here = max_ending_here + a[i]; 
        cnt += 1; 
        minSubarray = min(a[i], minSubarray); 
  
        # If sub-array sum is greater 
        # than the previous 
        if (sum == max_ending_here) :
  
            # If elements are 0, no removal 
            if (cnt == 1) :
                mini = min(mini, 0); 
  
            # If elements are more, then store 
            # the minimum value in the sub-array 
            # obtained till now 
            else :
                mini = min(mini, minSubarray); 
          
        # If sum is negative 
        if (max_ending_here < 0) :
  
            # Re-initialize everything 
            max_ending_here = 0; 
            cnt = 0; 
            minSubarray = sys.maxsize; 
  
    return sum - mini; 
  
# Driver code 
if __name__ == "__main__" : 
  
    a = [ 1, 2, 3, -2, 3 ]; 
    n = len(a)
      
    print(maximizeSum(a, n)); 
  
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
  
class GFG
{
      
// Function to return the maximum sub-array sum
static int maxSubArraySum(int []a, int size)
{
  
    // Initialized
    int max_so_far = int.MinValue, 
        max_ending_here = 0;
  
    // Traverse in the array
    for (int i = 0; i < size; i++)
    {
  
        // Increase the sum
        max_ending_here = max_ending_here + a[i];
  
        // If sub-array sum is more than the previous
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;
  
        // If sum is negative
        if (max_ending_here < 0)
            max_ending_here = 0;
    }
    return max_so_far;
}
  
// Function that returns the maximum sub-array sum
// after removing an element from the same sub-array
static int maximizeSum(int []a, int n)
{
    int cnt = 0;
    int mini = int.MaxValue;
    int minSubarray = int.MaxValue;
  
    // Maximum sub-array sum 
    // using Kadane's Algorithm
    int sum = maxSubArraySum(a, n);
  
    int max_so_far = int.MinValue, 
        max_ending_here = 0;
  
    // Re-apply Kadane's with minor changes
    for (int i = 0; i < n; i++)
    {
  
        // Increase the sum
        max_ending_here = max_ending_here + a[i];
        cnt++;
        minSubarray = Math.Min(a[i], minSubarray);
  
        // If sub-array sum is greater than the previous
        if (sum == max_ending_here)
        {
  
            // If elements are 0, no removal
            if (cnt == 1)
                mini = Math.Min(mini, 0);
  
            // If elements are more, then store
            // the minimum value in the sub-array
            // obtained till now
            else
                mini = Math.Min(mini, minSubarray);
        }
  
        // If sum is negative
        if (max_ending_here < 0)
        {
  
            // Re-initialize everything
            max_ending_here = 0;
            cnt = 0;
            minSubarray = int.MaxValue;
        }
    }
  
    return sum - mini;
}
  
// Driver code
public static void Main(String[] args)
{
    int []a = { 1, 2, 3, -2, 3 };
    int n = a.Length;
    Console.WriteLine(maximizeSum(a, n));
}
}
  
// This code has been contributed by 29AjayKumar


PHP


输出:
9