通过在范围 [L, R] 中为 Q 查询添加 X 来最大化给定数组的子数组和
给定一个包含N个整数的数组arr[]和M个(L, R, X)类型的更新查询,任务是在每个更新查询之后找到最大子数组和,其中在每个查询中,将整数X添加到数组arr[]在[L, R]范围内。
例子:
Input: arr[] = {-1, 5, -2, 9, 3, -3, 2}, query[] = {{0, 2, -10}, {4, 5, 2}}
Output: 12 15
Explanation: Below are the steps to solve the above example:
- The array after 1st update query becomes arr[] = {-11, -5, -12, 9, 3, -3, 2}. Hence the maximum subarray sum is 12 of the subarray arr[3… 4].
- The array after 2nd update query becomes arr[] = {-11, -5, -12, 9, 5, -1, 2}. Hence the maximum subarray sum is 15 of the subarray arr[3… 6].
Input: arr[] = {-2, -5, 6, -2, -3, 1, 5, -6, 4, -1}, query[] = {{1, 4, 3}, {4, 5, -4}, {7, 9, 5}}
Output: 16 10 20
方法:给定的问题可以使用 Kadane 算法来解决。对于每个查询,通过遍历数组arr[]范围[L, R]中的所有元素来更新数组元素,并将整数X添加到每个元素。每次更新查询后,使用此处讨论的算法计算最大子数组和。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum subarray
// sum using Kadane's Algorithm
int maxSubarraySum(int arr[], int n)
{
// Stores the maximum sum
int maxSum = INT_MIN;
int currSum = 0;
// Loop to iterate over the array
for (int i = 0; i <= n - 1; i++) {
currSum += arr[i];
// Update maxSum
if (currSum > maxSum) {
maxSum = currSum;
}
if (currSum < 0) {
currSum = 0;
}
}
// Return Answer
return maxSum;
}
// Function to add integer X to all elements
// of the given array in range [L, R]
void updateArr(int* arr, int L, int R, int X)
{
// Loop to iterate over the range
for (int i = L; i <= R; i++) {
arr[i] += X;
}
}
// Function to find the maximum subarray sum
// after each range update query
void maxSubarraySumQuery(
int arr[], int n,
vector > query)
{
// Loop to iterate over the queries
for (int i = 0; i < query.size(); i++) {
// Function call to update the array
// according to the mentioned query
updateArr(arr, query[i][0],
query[i][1],
query[i][2]);
// Print the max subarray sum after
// updating the given array
cout << maxSubarraySum(arr, n) << " ";
}
}
// Driver Code
int main()
{
int arr[] = { -2, -5, 6, -2, -3,
1, 5, -6, 4, -1 };
int N = sizeof(arr) / sizeof(arr[0]);
vector > query{ { 1, 4, 3 },
{ 4, 5, -4 },
{ 7, 9, 5 } };
maxSubarraySumQuery(arr, N, query);
return 0;
}
Java
// Java program for the above approach
class GFG {
// Function to find the maximum subarray
// sum using Kadane's Algorithm
public static int maxSubarraySum(int arr[], int n)
{
// Stores the maximum sum
int maxSum = Integer.MIN_VALUE;
int currSum = 0;
// Loop to iterate over the array
for (int i = 0; i <= n - 1; i++) {
currSum += arr[i];
// Update maxSum
if (currSum > maxSum) {
maxSum = currSum;
}
if (currSum < 0) {
currSum = 0;
}
}
// Return Answer
return maxSum;
}
// Function to add integer X to all elements
// of the given array in range [L, R]
public static void updateArr(int[] arr, int L,
int R, int X)
{
// Loop to iterate over the range
for (int i = L; i <= R; i++) {
arr[i] += X;
}
}
// Function to find the maximum subarray sum
// after each range update query
public static void maxSubarraySumQuery(int arr[], int n, int[][] query)
{
// Loop to iterate over the queries
for (int i = 0; i < query.length; i++)
{
// Function call to update the array
// according to the mentioned query
updateArr(arr, query[i][0],
query[i][1],
query[i][2]);
// Print the max subarray sum after
// updating the given array
System.out.print(maxSubarraySum(arr, n) + " ");
}
}
// Driver Code
public static void main(String args[])
{
int arr[] = { -2, -5, 6, -2, -3,
1, 5, -6, 4, -1 };
int N = arr.length;
int[][] query = { { 1, 4, 3 }, { 4, 5, -4 }, { 7, 9, 5 } };
maxSubarraySumQuery(arr, N, query);
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# Python Program to implement
# the above approach
# Function to find the maximum subarray
# sum using Kadane's Algorithm
def maxSubarraySum(arr, n):
# Stores the maximum sum
maxSum = 10 ** -9
currSum = 0
# Loop to iterate over the array
for i in range(n):
currSum += arr[i]
# Update maxSum
if (currSum > maxSum):
maxSum = currSum
if (currSum < 0):
currSum = 0
# Return Answer
return maxSum
# Function to add integer X to all elements
# of the given array in range[L, R]
def updateArr(arr, L, R, X):
# Loop to iterate over the range
for i in range(L, R + 1):
arr[i] += X
# Function to find the maximum subarray sum
# after each range update query
def maxSubarraySumQuery(arr, n, query):
# Loop to iterate over the queries
for i in range(len(query)):
# Function call to update the array
# according to the mentioned query
updateArr(arr, query[i][0],
query[i][1],
query[i][2])
# Print the max subarray sum after
# updating the given array
print(maxSubarraySum(arr, n), end=" ")
# Driver Code
arr = [-2, -5, 6, -2, -3, 1, 5, -6, 4, -1]
N = len(arr)
query = [[1, 4, 3],[4, 5, -4],[7, 9, 5]]
maxSubarraySumQuery(arr, N, query)
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the maximum subarray
// sum using Kadane's Algorithm
public static int maxSubarraySum(int[] arr, int n)
{
// Stores the maximum sum
int maxSum = int.MinValue;
int currSum = 0;
// Loop to iterate over the array
for (int i = 0; i <= n - 1; i++)
{
currSum += arr[i];
// Update maxSum
if (currSum > maxSum)
{
maxSum = currSum;
}
if (currSum < 0)
{
currSum = 0;
}
}
// Return Answer
return maxSum;
}
// Function to add integer X to all elements
// of the given array in range [L, R]
public static void updateArr(int[] arr, int L,
int R, int X)
{
// Loop to iterate over the range
for (int i = L; i <= R; i++)
{
arr[i] += X;
}
}
// Function to find the maximum subarray sum
// after each range update query
public static void maxSubarraySumQuery(int[] arr, int n, int[,] query)
{
// Loop to iterate over the queries
for (int i = 0; i < query.Length; i++)
{
// Function call to update the array
// according to the mentioned query
updateArr(arr, query[i, 0],
query[i, 1],
query[i, 2]);
// Print the max subarray sum after
// updating the given array
Console.Write(maxSubarraySum(arr, n) + " ");
}
}
// Driver Code
public static void Main()
{
int[] arr = { -2, -5, 6, -2, -3, 1, 5, -6, 4, -1 };
int N = arr.Length;
int[,] query = { { 1, 4, 3 }, { 4, 5, -4 }, { 7, 9, 5 } };
maxSubarraySumQuery(arr, N, query);
}
}
// This code is contributed by _saurabh_jaiswal.
Javascript
输出
16 10 20
时间复杂度: O(N*M)
辅助空间: O(1)