📌  相关文章
📜  将数组拆分为子数组,使其最大值和最小值之间的差值之和最大

📅  最后修改于: 2021-09-05 09:00:39             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是将数组拆分为子数组,使得所有子数组的最大和最小元素之间的差值之和最大。

例子

方法:给定的问题可以通过使用动态规划来解决。请按照以下步骤解决问题:

  • 初始化一个数组,比如dp[] ,其中dp[i]表示第i 个数组元素的所有子数组的最大和最小元素之差的最大和。
  • dp[0]初始化为0
  • [1, N – 1]范围内遍历给定数组并执行以下步骤:
    • 初始化一个变量,比如minarr[i] ,它存储[0, i]范围内的最小元素。
    • 初始化一个变量,比如max作为arr[i] ,它存储[0, i]范围内的最大元素。
    • 使用变量j以相反的顺序迭代范围[0, i]并执行以下步骤:
      • min的值更新为minarr[j]的最小值。
      • max的值更新为maxarr[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