给定一个由N 个整数组成的数组arr[] ,任务是将数组拆分为子数组,使得所有子数组的最大和最小元素之间的差值之和最大。
例子:
Input: arr[] = {8, 1, 7, 9, 2}
Output: 14
Explanation:
Consider splitting the given array into subarrays as {8, 1} and {7, 9, 2}. Now, the difference between maximum and minimum elements are:
- {8, 1}: Difference is (8 – 1) = 7.
- {7, 9, 2}: Difference is (9 – 2) = 7.
Therefore, the sum of the difference is 7 + 7 = 14.
Input: arr[] = {1, 2, 1, 0, 5}
Output: 6
方法:给定的问题可以通过使用动态规划来解决。请按照以下步骤解决问题:
- 初始化一个数组,比如dp[] ,其中dp[i]表示第i 个数组元素的所有子数组的最大和最小元素之差的最大和。
- 将dp[0]初始化为0 。
- 在[1, N – 1]范围内遍历给定数组并执行以下步骤:
- 初始化一个变量,比如min为arr[i] ,它存储[0, i]范围内的最小元素。
- 初始化一个变量,比如max作为arr[i] ,它存储[0, i]范围内的最大元素。
- 使用变量j以相反的顺序迭代范围[0, i]并执行以下步骤:
- 将min的值更新为min和arr[j]的最小值。
- 将max的值更新为max和arr[j]的最小值。
- 将dp[j]的值更新为dp[j]和(max – min + dp[i]) 的最大值。
- 完成以上步骤后,打印dp[N-1]的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find maximum sum of
// difference between maximums and
// minimums in the splitted subarrays
int getValue(int arr[], int N)
{
int dp[N];
memset(dp, 0, sizeof(dp));
// Base Case
dp[0] = 0;
// Traverse the array
for(int i = 1; i < N; i++)
{
// Stores the maximum and
// minimum elements upto
// the i-th index
int minn = arr[i];
int maxx = arr[i];
// Traverse the range [0, i]
for(int j = i; j >= 0; j--)
{
// Update the minimum
minn = min(arr[j], minn);
// Update the maximum
maxx = max(arr[j], maxx);
// Update dp[i]
dp[i] = max(dp[i], maxx - minn +
((j >= 1) ? dp[j - 1] : 0));
}
}
// Return the maximum
// sum of difference
return dp[N - 1];
}
// Driver code
int main()
{
int arr[] = { 8, 1, 7, 9, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << getValue(arr, N);
return 0;
}
// This code is contributed by Kingash
Java
// Java program for the above approach
import java.util.*;
public class Main {
// Function to find maximum sum of
// difference between maximums and
// minimums in the splitted subarrays
static int getValue(int[] arr, int N)
{
int dp[] = new int[N];
// Base Case
dp[0] = 0;
// Traverse the array
for (int i = 1; i < N; i++) {
// Stores the maximum and
// minimum elements upto
// the i-th index
int min = arr[i];
int max = arr[i];
// Traverse the range [0, i]
for (int j = i; j >= 0; j--) {
// Update the minimum
min = Math.min(arr[j], min);
// Update the maximum
max = Math.max(arr[j], max);
// Update dp[i]
dp[i] = Math.max(
dp[i],
max - min + ((j >= 1)
? dp[j - 1]
: 0));
}
}
// Return the maximum
// sum of difference
return dp[N - 1];
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 8, 1, 7, 9, 2 };
int N = arr.length;
System.out.println(getValue(arr, N));
}
}
C#
// C# program for the above approach
using System;
class GFG{
// Function to find maximum sum of
// difference between maximums and
// minimums in the splitted subarrays
static int getValue(int[] arr, int N)
{
int[] dp = new int[N];
// Base Case
dp[0] = 0;
// Traverse the array
for(int i = 1; i < N; i++)
{
// Stores the maximum and
// minimum elements upto
// the i-th index
int min = arr[i];
int max = arr[i];
// Traverse the range [0, i]
for(int j = i; j >= 0; j--)
{
// Update the minimum
min = Math.Min(arr[j], min);
// Update the maximum
max = Math.Max(arr[j], max);
// Update dp[i]
dp[i] = Math.Max(
dp[i],
max - min + ((j >= 1) ?
dp[j - 1] : 0));
}
}
// Return the maximum
// sum of difference
return dp[N - 1];
}
// Driver Code
static public void Main()
{
int[] arr = { 8, 1, 7, 9, 2 };
int N = arr.Length;
Console.Write(getValue(arr, N));
}
}
// This code is contributed by code_hunt
Python3
# python 3 program for the above approach
# Function to find maximum sum of
# difference between maximums and
# minimums in the splitted subarrays
def getValue(arr, N):
dp = [0 for i in range(N)]
# Traverse the array
for i in range(1, N):
# Stores the maximum and
# minimum elements upto
# the i-th index
minn = arr[i]
maxx = arr[i]
j = i
# Traverse the range [0, i]
while(j >= 0):
# Update the minimum
minn = min(arr[j], minn)
# Update the maximum
maxx = max(arr[j], maxx)
# Update dp[i]
dp[i] = max(dp[i], maxx - minn + (dp[j - 1] if (j >= 1) else 0))
j -= 1
# Return the maximum
# sum of difference
return dp[N - 1]
# Driver code
if __name__ == '__main__':
arr = [8, 1, 7, 9, 2]
N = len(arr)
print(getValue(arr, N))
# This code is contributed by SURENDRA_GANGWAR.
Javascript
输出:
14
时间复杂度: O(N 2 )
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live