📌  相关文章
📜  将给定数组拆分为子数组以最大化每个子数组中的最大值和最小值之和

📅  最后修改于: 2022-05-13 01:56:04.598000             🧑  作者: Mango

将给定数组拆分为子数组以最大化每个子数组中的最大值和最小值之和

给定一个由N个整数组成的数组arr[] ,任务是通过将给定数组拆分为不重叠的子数组来最大化每个子数组中最大值和最小值之差的总和。

例子:

方法:给定的问题可以通过考虑所有可能的子数组破坏来解决,并找到每个子数组中最大值和最小值之差的总和并将该值最大化。这个想法可以使用动态编程来实现。请按照以下步骤解决给定的问题:

  • 初始化一个数组dp[] ,所有元素都为0 ,这样dp[i]存储前i个元素的所有可能拆分的总和,使得每个子数组中的最大值和最小值之差的总和最大。
  • 使用变量i遍历给定的数组arr[]并执行以下步骤:
    • 选择当前值作为子数组的最大值和最小值,并将其存储在变量中,分别表示minValuemaxValue
    • 使用变量j[0, i]范围内迭代循环并执行以下步骤:
      • 使用数组元素arr[j]更新minValuemaxValue
      • 如果j的值为正,则将dp[i]更新为dp[i](maxValue – minValue + dp[j – 1])的最大值。否则,将dp[i]更新为dp[i](maxValue – minValue)的最大值。
  • 完成上述步骤后,打印dp[N – 1]的值作为结果最大值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum sum of
// differences of subarrays by splitting
// array into non-overlapping subarrays
int maxDiffSum(int arr[], int n)
{
    // Stores the answer for prefix
    // and initialize with zero
    int dp[n];
    memset(dp, 0, sizeof(dp));
 
    // Assume i-th index as right endpoint
    for (int i = 0; i < n; i++) {
 
        // Choose the current value as
        // the maximum and minimum
        int maxVal = arr[i], minVal = arr[i];
 
        // Find the left endpoint and
        // update the array dp[]
        for (int j = i; j >= 0; j--) {
            minVal = min(minVal, arr[j]);
            maxVal = max(maxVal, arr[j]);
 
            if (j - 1 >= 0)
                dp[i] = max(
                    dp[i], maxVal - minVal + dp[j - 1]);
            else
                dp[i] = max(
                    dp[i], maxVal - minVal);
        }
    }
 
    // Return answer
    return dp[n - 1];
}
 
// Driver Code
int main()
{
    int arr[] = { 8, 1, 7, 9, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << maxDiffSum(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG {
 
    // Function to find the maximum sum of
    // differences of subarrays by splitting
    // array into non-overlapping subarrays
    static int maxDiffSum(int[] arr, int n)
    {
       
        // Stores the answer for prefix
        // and initialize with zero
        int[] dp = new int[n];
 
        // Assume i-th index as right endpoint
        for (int i = 0; i < n; i++) {
 
            // Choose the current value as
            // the maximum and minimum
            int maxVal = arr[i], minVal = arr[i];
 
            // Find the left endpoint and
            // update the array dp[]
            for (int j = i; j >= 0; j--) {
                minVal = Math.min(minVal, arr[j]);
                maxVal = Math.max(maxVal, arr[j]);
 
                if (j - 1 >= 0)
                    dp[i] = Math.max(
                        dp[i], maxVal - minVal + dp[j - 1]);
                else
                    dp[i]
                        = Math.max(dp[i], maxVal - minVal);
            }
        }
 
        // Return answer
        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.print(maxDiffSum(arr, N));
    }
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python Program to implement
# the above approach
 
# Function to find the maximum sum of
# differences of subarrays by splitting
# array into non-overlapping subarrays
def maxDiffSum(arr, n):
 
    # Stores the answer for prefix
    # and initialize with zero
    dp = [0] * n
 
    # Assume i-th index as right endpoint
    for i in range(n):
 
        # Choose the current value as
        # the maximum and minimum
        maxVal = arr[i]
        minVal = arr[i]
 
        # Find the left endpoint and
        # update the array dp[]
        for j in range(i, -1, -1):
            minVal = min(minVal, arr[j])
            maxVal = max(maxVal, arr[j])
 
            if (j - 1 >= 0):
                dp[i] = max(
                    dp[i], maxVal - minVal + dp[j - 1])
            else:
                dp[i] = max(
                    dp[i], maxVal - minVal)
 
    # Return answer
    return dp[n - 1]
 
# Driver Code
arr = [8, 1, 7, 9, 2]
N = len(arr)
 
print(maxDiffSum(arr, N))
 
# This code is contributed by _saurabh_jaiswal


C#
// C# program for the above approach
using System;
class GFG {
 
    // Function to find the maximum sum of
    // differences of subarrays by splitting
    // array into non-overlapping subarrays
    static int maxDiffSum(int[] arr, int n)
    {
       
        // Stores the answer for prefix
        // and initialize with zero
        int[] dp = new int[n];
 
        // Assume i-th index as right endpoint
        for (int i = 0; i < n; i++) {
 
            // Choose the current value as
            // the maximum and minimum
            int maxVal = arr[i], minVal = arr[i];
 
            // Find the left endpoint and
            // update the array dp[]
            for (int j = i; j >= 0; j--) {
                minVal = Math.Min(minVal, arr[j]);
                maxVal = Math.Max(maxVal, arr[j]);
 
                if (j - 1 >= 0)
                    dp[i] = Math.Max(
                        dp[i], maxVal - minVal + dp[j - 1]);
                else
                    dp[i]
                        = Math.Max(dp[i], maxVal - minVal);
            }
        }
 
        // Return answer
        return dp[n - 1];
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 8, 1, 7, 9, 2 };
        int N = arr.Length;
 
        Console.WriteLine(maxDiffSum(arr, N));
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
14

时间复杂度: O(N 2 )
辅助空间: O(N)