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

📅  最后修改于: 2021-09-07 02:30:20             🧑  作者: Mango

给定一个大小为N的升序排序数组arr[]和一个整数K ,任务是将给定的数组划分为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


Javascript


输出:
12

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live