给定一个大小为N的升序排序数组arr[]和一个整数K ,任务是将给定的数组划分为K 个非空子数组,使得每个子数组的最大值和最小值的差之和最小。
例子:
Input: arr[] = {4, 8, 15, 16, 23, 42}, K = 3
Output: 12
Explanation:
The given array can be split into three sub arrays in the following way:
{4, 8}, {15, 16, 23}, {42}
Here, the sum of difference of the minimum and maximum element in each of the subarrays respectively are:
4 + 8 + 0 = 12.
Input: arr[] = {1, 2, 3, 4, 5}, K = 5
Output: 0
处理方法:需要做的一个观察是,我们清楚地知道,只有当我们选择相邻元素作为子数组的最大和最小元素时,我们才能清楚地知道子数组的最大和最小元素之间的差值之和最小。所以:
- 假设我们必须将数组分成 K + 1 个部分,这样第一部分将是 arr[0 … i 1 -1],第二部分将是 arr[i 1 … i 2 -1],依此类推。
- 因此,K 部分之间的差异总和将是:
sum = arr[i1-1] – arr[0] + arr[i2-1] – arr[i1] + …. + arr[n] – arr[iK]
- 重新排列上述值后,我们得到:
sum = -arr[0] – (arr[i1] – arr[i1 – 1]) – (arr[i2] – arr[i2 – 1]) – … -(arr[iK] – arr[iK – 1]) + arr[N – 1]
- 显然,要计算的值是由数组的相邻元素之间的差异形成的。如果该差异最大,则总和将最小。
- 因此,想法是遍历数组并将数组的相邻元素之间的差异存储在另一个数组中。
- 现在,按降序对这个数组进行排序。我们知道应该取差异的最大值以获得最小差异。
- 因此,从数组的第一个和最后一个元素的差值中减去前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