📌  相关文章
📜  将排序的数组划分为K个部分,每个部分的最大和最小差之和最小

📅  最后修改于: 2021-05-19 19:20:59             🧑  作者: Mango

给定大小为N且整数K的升序排列的数组arr [] ,任务是将给定的数组划分为K个非空子数组,以使每个子数组的最大值和最小值之和最小。

例子:

方法:需要做的一项观察是,我们清楚地知道,只有当我们选择相邻元素作为子数组的最大和最小元素时,子数组的最大和最小元素之间的差之和才最小。所以:

  • 假设我们必须将数组拆分为K + 1个部分,以使第一部分为arr [0…i 1 -1],第二部分为arr [i 1 …i 2 -1],依此类推。
  • 因此,K个部分之间的差异之和为:
  • 重新排列以上值后,我们得到:
  • 显然,要计算的值是由数组的相邻元素之间的差形成的。如果此差异最大,则总和将最小。
  • 因此,该想法是遍历数组并将该数组的相邻元素之间的差异存储在另一个数组中。
  • 现在,按降序对该数组排序。我们知道应该采用差异的最大值来获得最小的差异。
  • 因此,从数组的第一个元素和最后一个元素的差中减去前K个-1 。这给出了由该阵列形成的K个子阵列的剩余差的总和。

下面是上述方法的实现:

C++
// C++ program to find the minimum
// sum of differences possible for
// the given array when the array
// is divided into K subarrays
#include
using namespace std;
 
// Function to find the minimum
// sum of differences possible for
// the given array when the array
// is divided into K subarrays
int calculate_minimum_split(int n, int a[], int k)
{
 
    // Array to store the differences
    // between two adjacent elements
    int p[n - 1];
     
    // Iterating through the array
    for(int i = 1; i < n; i++)
     
        // Storing differences to p
        p[i - 1] = a[i] - a[i - 1];
         
    // Sorting p in descending order
    sort(p, p + n - 1, greater());
     
    // Sum of the first k-1 values of p
    int min_sum = 0;
    for(int i = 0; i < k - 1; i++)
        min_sum += p[i];
         
    // Computing the result
    int res = a[n - 1] - a[0] - min_sum;
     
    return res;
}
 
// Driver code
int main()
{
    int arr[6] = { 4, 8, 15, 16, 23, 42 };
    int k = 3;
    int n = sizeof(arr) / sizeof(int);
 
    cout << calculate_minimum_split(n, arr, k);
}
 
// This code is contributed by ishayadav181


Java
// Java program to find the minimum
// sum of differences possible for
// the given array when the array
// is divided into K subarrays
import java.util.*;
 
class GFG{
 
// Function to find the minimum
// sum of differences possible for
// the given array when the array
// is divided into K subarrays
static int calculate_minimum_split(int n, int a[],
                                   int k)
{
     
    // Array to store the differences
    // between two adjacent elements
    Integer[] p = new Integer[n - 1];
 
    // Iterating through the array
    for(int i = 1; i < n; i++)
 
        // Storing differences to p
        p[i - 1] = a[i] - a[i - 1];
 
    // Sorting p in descending order
    Arrays.sort(p, new Comparator()
    {
        public int compare(Integer a, Integer b)
        {
            return b - a;
        }
    });
 
    // Sum of the first k-1 values of p
    int min_sum = 0;
    for(int i = 0; i < k - 1; i++)
        min_sum += p[i];
 
    // Computing the result
    int res = a[n - 1] - a[0] - min_sum;
 
    return res;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 4, 8, 15, 16, 23, 42 };
    int k = 3;
    int n = arr.length;
 
    System.out.println(calculate_minimum_split(
                       n, arr, k));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to find the minimum
# sum of differences possible for
# the given array when the array
# is divided into K subarrays
 
# Function to find the minimum
# sum of differences possible for
# the given array when the array
# is divided into K subarrays
def calculate_minimum_split(a, k):
 
    # Array to store the differences
    # between two adjacent elements
    p =[]
    n = len(a)
 
    # Iterating through the array
    for i in range(1, n):
         
        # Appending differences to p
        p.append(a[i]-a[i-1])
 
    # Sorting p in descending order
    p.sort(reverse = True)
  
    # Sum of the first k-1 values of p
    min_sum = sum(p[:k-1])
  
    # Computing the result
    res = a[n-1]-a[0]-min_sum
     
    return res
 
if __name__ == "__main__":
    arr = [4, 8, 15, 16, 23, 42]
    K = 3
 
    print(calculate_minimum_split(arr, K))


C#
// C# program to find the minimum 
// sum of differences possible for 
// the given array when the array 
// is divided into K subarrays 
using System;
 
class GFG{
     
// Function to find the minimum 
// sum of differences possible for 
// the given array when the array 
// is divided into K subarrays 
static int calculate_minimum_split(int n, int[] a,
                                   int k) 
{ 
     
    // Array to store the differences 
    // between two adjacent elements 
    int[] p = new int[n - 1]; 
       
    // Iterating through the array 
    for(int i = 1; i < n; i++) 
       
        // Storing differences to p 
        p[i - 1] = a[i] - a[i - 1]; 
           
    // Sorting p in descending order 
    Array.Sort(p);
    Array.Reverse(p);
       
    // Sum of the first k-1 values of p 
    int min_sum = 0; 
    for(int i = 0; i < k - 1; i++) 
        min_sum += p[i]; 
           
    // Computing the result 
    int res = a[n - 1] - a[0] - min_sum; 
       
    return res; 
} 
 
// Driver code
static void Main()
{
    int[] arr = { 4, 8, 15, 16, 23, 42 }; 
    int k = 3; 
    int n = arr.Length; 
 
    Console.Write(calculate_minimum_split(
                  n, arr, k));
}
}
 
// This code is contributed by divyeshrabadiya07


输出:
12

时间复杂度: O(N * log(N)) ,其中N是数组的大小。