给定一个大小为N的数组arr[] ,任务是计算通过将数组划分为多个子数组(可能是一个)可以获得的最大和,其中每个子数组从索引i开始到索引j结束(j>= i)将arr[j]-arr[i]贡献给总和。
例子:
Input: arr[]= {1, 5, 3}, N=3
Output:
4
Explanation: The array can be divided into 2 subarrays:
- {1, 5} -> sum contributed by the subarray = 5-1 = 4
- {3} -> sum contributed by the subarray = 3-3 = 0
Therefore, the answer is 4.(It can be shown that there is no other way of dividing this array in multiple subarrays such that the answer is greater than 4).
Input: arr[] = {6, 2, 1}, N=3
Output:
0
朴素的方法:朴素的方法是考虑将arr划分为 1 个或多个子数组并计算每个子数组获得的最大和的所有可能方法。
时间复杂度: O(N*2 N )
辅助空间: O(1)
观察:解决问题所需的观察如下:
- 数组应该被分成几个(可能是一个)子数组,这样每个子数组都是最长的递增子数组。例如,如果arr[]={3,5,7,9,1} ,最好将{3,5,7,9}视为子数组,因为它将对总和做出9-3=6 的贡献。将其分解进一步减少了不是最佳的总和。
- 非递增子数组的每个元素都应视为单元素子数组,以便它们对总和的贡献为 0。否则,他们将贡献一个负值。例如,如果arr[i]>arr[i+1],最好分别考虑包含arr[i]和arr[i+1] 的两个长度为 1 的子数组,以便它们贡献(arr[i]- arr[i]) +(arr[i+1]-arr[i+1]) =0到答案。如果将它们放在一起考虑,它们将贡献arr[i+1]-arr[i] ,这是一个负数,从而减少总和。
有效方法:按照以下步骤解决问题:
- 将变量Sum初始化为 0。
- 使用变量i将arr从1遍历到N-1 ,并执行以下操作:
- 如果arr[i]>arr[i-1] ,将arr[i]-arr[i-1] 添加到Sum 。这是有效的,因为排序数组中相邻元素的差值之和等于最末端元素的差值。这里,只有增加的子数组被认为是arr[i]>arr[i-1]。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the required answer
int maximumSum(int arr[], int N)
{
// Stores maximum sum
int Sum = 0;
for (int i = 1; i < N; i++) {
// Adding the difference of elements at ends of
// increasing subarray to the answer
if (arr[i] > arr[i - 1])
Sum += (arr[i] - arr[i - 1]);
}
return Sum;
}
// Driver Code
int main()
{
// Input
int arr[] = { 1, 5, 3 };
int N = (sizeof(arr) / (sizeof(arr[0])));
// Functio calling
cout << maximumSum(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to find the required answer
public static int maximumSum(int arr[], int N)
{
// Stores maximum sum
int Sum = 0;
for(int i = 1; i < N; i++)
{
// Adding the difference of elements at ends
// of increasing subarray to the answer
if (arr[i] > arr[i - 1])
Sum += (arr[i] - arr[i - 1]);
}
return Sum;
}
// Driver Code
public static void main(String[] args)
{
// Input
int arr[] = { 1, 5, 3 };
int N = arr.length;
// Function calling
System.out.println(maximumSum(arr, N));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python program for the above approach
# Function to find the required answer
def maximumSum(arr, N):
# Stores maximum sum
Sum = 0;
for i in range(1,N):
# Adding the difference of elements at ends of
# increasing subarray to the answer
if (arr[i] > arr[i - 1]):
Sum += (arr[i] - arr[i - 1])
return Sum;
# Driver Code
#Input
arr = [ 1, 5, 3 ];
N = len(arr)
# Functio calling
print(maximumSum(arr, N));
# This code is contributed by SoumikMondal
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the required answer
static int maximumSum(int []arr, int N)
{
// Stores maximum sum
int Sum = 0;
for(int i = 1; i < N; i++)
{
// Adding the difference of elements at
// ends of increasing subarray to the answer
if (arr[i] > arr[i - 1])
Sum += (arr[i] - arr[i - 1]);
}
return Sum;
}
// Driver Code
public static void Main()
{
// Input
int []arr = { 1, 5, 3 };
int N = arr.Length;
// Functio calling
Console.Write(maximumSum(arr, N));
}
}
// This code is contributed by SURENDRA_GANGWAR
Javascript
输出
4
时间复杂度: O(N)
辅助空间: O(1)