给定一个包含 N 个整数的数组。任务是找到具有最小(最小)和的连续子数组的元素之和。
例子:
Input: arr[] = {3, -4, 2, -3, -1, 7, -5}
Output:-6
Input: arr = {2, 6, 8, 1, 4}
Output: 1
一种方法已经在上一篇文章中讨论过。在这篇文章中,讨论了使用最大和连续子阵列方法的解决方案。这是基于这样一个事实:为了找到最小连续和,我们可以首先使原始数组的元素为负,即。将每个 ar[i] 替换为 -ar[i],然后应用 Kadane 算法。显然,如果这是形成的最大总和,那么最小总和将是该总和的负数。
下面是上述方法的实现:
C++
// C++ program for
// Smallest sum contiguous subarray | Set 2
#include
using namespace std;
// function to find the smallest sum contiguous subarray
int smallestSumSubarr(int arr[], int n)
{
// First invert the sign of the elements
for (int i = 0; i < n; i++)
arr[i] = -arr[i];
// Apply the normal Kadane algorithm But on the elements
// Of the array having inverted sign
int sum_here = arr[0], max_sum = arr[0];
for (int i = 1; i < n; i++) {
sum_here = max(sum_here + arr[i], arr[i]);
max_sum = max(max_sum, sum_here);
}
// Invert the answer to get minimum val
return (-1) * max_sum;
}
// Driver Code
int main()
{
int arr[] = { 3, -4, 2, -3, -1, 7, -5 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Smallest sum: "
<< smallestSumSubarr(arr, n);
return 0;
}
Java
// Java program for Smallest
// sum contiguous subarray | Set 2
import java.io.*;
class GFG
{
// function to find the
// smallest sum contiguous
// subarray
static int smallestSumSubarr(int arr[],
int n)
{
// First invert the
// sign of the elements
for (int i = 0; i < n; i++)
arr[i] = -arr[i];
// Apply the normal Kadane
// algorithm But on the
// elements Of the array
// having inverted sign
int sum_here = arr[0],
max_sum = arr[0];
for (int i = 1; i < n; i++)
{
sum_here = Math.max(sum_here +
arr[i], arr[i]);
max_sum = Math.max(max_sum,
sum_here);
}
// Invert the answer
// to get minimum val
return (-1) * max_sum;
}
// Driver Code
public static void main (String[] args)
{
int arr[] = {3, -4, 2, -3,
-1, 7, -5};
int n = arr.length;
System.out.print("Smallest sum: " +
smallestSumSubarr(arr, n));
}
}
// This code is contributed
// by inder_verma.
Python3
# Python3 program for
# Smallest sum contiguous subarray | Set 2
# function to find the smallest
# sum contiguous subarray
def smallestSumSubarr(arr, n):
# First invert the sign of the elements
for i in range(n):
arr[i] = -arr[i]
# Apply the normal Kadane algorithm but
# on the elements of the array having inverted sign
sum_here = arr[0]
max_sum = arr[0]
for i in range(1, n):
sum_here = max(sum_here + arr[i], arr[i])
max_sum = max(max_sum, sum_here)
# Invert the answer to get minimum val
return (-1) * max_sum
# Driver Code
arr = [3, -4, 2, -3, -1, 7, -5]
n = len(arr)
print("Smallest sum:",
smallestSumSubarr(arr, n))
# This code is contributed by Mohit Kumar
C#
// C# program for Smallest
// sum contiguous subarray | Set 2
using System;
class GFG
{
// function to find the
// smallest sum contiguous
// subarray
static int smallestSumSubarr(int []arr,
int n)
{
// First invert the
// sign of the elements
for (int i = 0; i < n; i++)
arr[i] = -arr[i];
// Apply the normal Kadane
// algorithm But on the
// elements Of the array
// having inverted sign
int sum_here = arr[0],
max_sum = arr[0];
for (int i = 1; i < n; i++)
{
sum_here = Math.Max(sum_here +
arr[i], arr[i]);
max_sum = Math.Max(max_sum,
sum_here);
}
// Invert the answer
// to get minimum val
return (-1) * max_sum;
}
// Driver Code
public static void Main ()
{
int []arr = {3, -4, 2, -3,
-1, 7, -5};
int n = arr.Length;
Console.WriteLine("Smallest sum: " +
smallestSumSubarr(arr, n));
}
}
// This code is contributed
// by inder_verma.
PHP
Javascript
输出:
Smallest sum: -6
时间复杂度: O(n)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。