📌  相关文章
📜  将数组拆分为K个子数组,相邻元素之间的绝对差的总和最小

📅  最后修改于: 2021-05-17 21:36:29             🧑  作者: Mango

给定大小为N且整数K的数组arr [] ,任务是将数组拆分为K个子数组,以最小化每个子数组的相邻元素之间的绝对差之和。

例子:

方法:可以根据以下观察结果解决给定问题:

请按照以下步骤解决问题:

  1. 初始化一个数组,例如new_Arr []和一个整数变量,例如ans ,以存储总的绝对差和。
  2. 遍历数组。
    • 将相邻元素的绝对差(例如arr [i + 1]arr [i])存储new_Arr []数组中。
    • 通过arr [i]arr [i + 1]的绝对差值增加ans
  3. 按降序对new_Arr []数组进行排序。
  4. i = 0遍历到i = K-1
    • new_Arr [i]减少ans
  5. 最后,打印出ans。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to split an array into K subarrays
// with minimum sum of absolute difference
// of adjacent elements in each of K subarrays
void absoluteDifference(int arr[], int N, int K)
{
 
    // Stores the absolute differences
    // of adjacent elements
    int new_Arr[N - 1];
 
    // Stores the answer
    int ans = 0;
 
    // Stores absolute differences of
    // adjacent elements in new_Arr
    for (int i = 0; i < N - 1; i++) {
        new_Arr[i] = abs(arr[i] - arr[i + 1]);
 
        // Stores the sum of all absolute
        // differences of adjacent elements
        ans += new_Arr[i];
    }
 
    // Sorting the new_Arr
    // in decreasing order
    sort(new_Arr, new_Arr + N - 1,
         greater());
 
    for (int i = 0; i < K - 1; i++) {
 
        // Removing K - 1 elements
        // with maximum sum
        ans -= new_Arr[i];
    }
 
    // Prints the answer
    cout << ans << endl;
}
 
// Driver code
int main()
{
    // Given array arr[]
    int arr[] = { 1, 3, -2, 5, -1 };
 
    // Given K
    int K = 2;
 
    // Size of array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    absoluteDifference(arr, N, K);
    return 0;
}


Java
//  java program for the above approach
import java.util.*;
import java.util.Arrays;
class GFG
{
     
 public static void reverse(int[] array)
 {
 
   // Length of the array
   int n = array.length;
 
   // Swaping the first half elements with last half
   // elements
   for (int i = 0; i < n / 2; i++)
   {
 
     // Storing the first half elements temporarily
     int temp = array[i];
 
     // Assigning the first half to the last half
     array[i] = array[n - i - 1];
 
     // Assigning the last half to the first half
     array[n - i - 1] = temp;
   }
 }
 
  // Function to split an array into K subarrays
  // with minimum sum of absolute difference
  // of adjacent elements in each of K subarrays
  public static void absoluteDifference(int arr[],
                                        int N, int K)
  {
 
    // Stores the absolute differences
    // of adjacent elements
    int new_Arr[] = new int[N - 1];
 
    // Stores the answer
    int ans = 0;
 
    // Stores absolute differences of
    // adjacent elements in new_Arr
    for (int i = 0; i < N - 1; i++)
    {
      new_Arr[i] = Math.abs(arr[i] - arr[i + 1]);
 
      // Stores the sum of all absolute
      // differences of adjacent elements
      ans += new_Arr[i];
    }
 
    // Sorting the new_Arr
    // in decreasing order
    Arrays.sort(new_Arr);
    reverse(new_Arr);
 
    for (int i = 0; i < K - 1; i++)
    {
 
      // Removing K - 1 elements
      // with maximum sum
      ans -= new_Arr[i];
    }
 
    // Prints the answer
    System.out.println(ans);
  }
 
  // Driver code
  public static void main (String[] args)
  {
 
    // Given array arr[]
    int arr[] = { 1, 3, -2, 5, -1 };
 
    // Given K
    int K = 2;
 
    // Size of array
    int N = arr.length;
 
    // Function Call
    absoluteDifference(arr, N, K);
   }
}
 
// This code is contributed by AnkThon


Python3
# Python3 program for the above approach
 
# Function to split an array into K subarrays
# with minimum sum of absolute difference
# of adjacent elements in each of K subarrays
def absoluteDifference(arr, N, K):
     
    # Stores the absolute differences
    # of adjacent elements
    new_Arr = [0 for i in range(N - 1)]
 
    # Stores the answer
    ans = 0
 
    # Stores absolute differences of
    # adjacent elements in new_Arr
    for i in range(N - 1):
        new_Arr[i] = abs(arr[i] - arr[i + 1])
 
        # Stores the sum of all absolute
        # differences of adjacent elements
        ans += new_Arr[i]
 
    # Sorting the new_Arr
    # in decreasing order
    new_Arr = sorted(new_Arr)[::-1]
 
    for i in range(K - 1):
 
        # Removing K - 1 elements
        # with maximum sum
        ans -= new_Arr[i]
 
    # Prints the answer
    print (ans)
 
# Driver code
if __name__ == '__main__':
 
    # Given array arr[]
    arr = [ 1, 3, -2, 5, -1 ]
 
    # Given K
    K = 2
 
    # Size of array
    N = len(arr)
 
    # Function Call
    absoluteDifference(arr, N, K)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
  
public static void reverse(int[] array)
{
     
    // Length of the array
    int n = array.Length;
     
    // Swaping the first half elements with
    // last half elements
    for(int i = 0; i < n / 2; i++)
    {
     
        // Storing the first half elements
        // temporarily
        int temp = array[i];
         
        // Assigning the first half to
        // the last half
        array[i] = array[n - i - 1];
         
        // Assigning the last half to
        // the first half
        array[n - i - 1] = temp;
    }
}
     
// Function to split an array into K subarrays
// with minimum sum of absolute difference
// of adjacent elements in each of K subarrays
public static void absoluteDifference(int[] arr,
                                      int N, int K)
{
     
    // Stores the absolute differences
    // of adjacent elements
    int[] new_Arr = new int[N - 1];
     
    // Stores the answer
    int ans = 0;
     
    // Stores absolute differences of
    // adjacent elements in new_Arr
    for(int i = 0; i < N - 1; i++)
    {
        new_Arr[i] = Math.Abs(arr[i] -
                              arr[i + 1]);
         
        // Stores the sum of all absolute
        // differences of adjacent elements
        ans += new_Arr[i];
    }
     
    // Sorting the new_Arr
    // in decreasing order
    Array.Sort(new_Arr);
    reverse(new_Arr);
     
    for(int i = 0; i < K - 1; i++)
    {
         
        // Removing K - 1 elements
        // with maximum sum
        ans -= new_Arr[i];
    }
     
    // Prints the answer
    Console.WriteLine(ans);
}
 
// Driver Code
public static void Main ()
{
     
    // Given array arr[]
    int[] arr = { 1, 3, -2, 5, -1 };
     
    // Given K
    int K = 2;
     
    // Size of array
    int N = arr.Length;
     
    // Function Call
    absoluteDifference(arr, N, K);
}
}
 
// This code is contributed by susmitakundugoaldanga


输出:
13

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