📌  相关文章
📜  将给定数组拆分为 K 个子数组,以最小化它们的最大值和最小值之间的差异

📅  最后修改于: 2021-09-16 11:15:40             🧑  作者: Mango

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

例子:

方法:要将给定的数组拆分为具有给定条件的K个子数组,其想法是在元素arr[i+1]arr[i]之间差异最大的索引(比如i )处进行拆分。以下是实现此方法的步骤:

  1. 将给定数组arr[] 中连续元素对之间的差异存储到另一个数组中(比如temp[] )。
  2. 按升序对数组temp[]进行排序。
  3. 将总差值(比如diff )初始化为给定数组arr[]的第一个和最后一个元素的差值。
  4. 将数组temp[] 中的前K – 1 个值与上述差值相加。
  5. diff 中存储的值是K子数组的最大和最小元素之差的最小和。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the subarray
int find(int a[], int n, int k)
{
    vector v;
 
    // Add the difference to vectors
    for (int i = 1; i < n; ++i) {
        v.push_back(a[i - 1] - a[i]);
    }
 
    // Sort vector to find minimum k
    sort(v.begin(), v.end());
 
    // Initialize result
    int res = a[n - 1] - a[0];
 
    // Adding first k-1 values
    for (int i = 0; i < k - 1; ++i) {
        res += v[i];
    }
 
// Return the minimized sum
    return res;
}
 
// Driver Code
int main()
{
// Given array arr[]
    int arr[] = { 4, 8, 15, 16, 23, 42 };
 
    int N = sizeof(arr) / sizeof(int);
 
// Given K
int K = 3;
 
// Function Call
    cout << find(arr, N, K) << endl;
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the subarray
static int find(int a[], int n, int k)
{
    Vector v = new Vector();
 
    // Add the difference to vectors
    for(int i = 1; i < n; ++i)
    {
       v.add(a[i - 1] - a[i]);
    }
 
    // Sort vector to find minimum k
    Collections.sort(v);
 
    // Initialize result
    int res = a[n - 1] - a[0];
 
    // Adding first k-1 values
    for(int i = 0; i < k - 1; ++i)
    {
       res += v.get(i);
    }
     
    // Return the minimized sum
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given array arr[]
    int arr[] = { 4, 8, 15, 16, 23, 42 };
 
    int N = arr.length;
     
    // Given K
    int K = 3;
     
    // Function Call
    System.out.print(find(arr, N, K) + "\n");
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Function to find the subarray
def find(a, n, k):
     
    v = []
     
    # Add the difference to vectors
    for i in range(1, n):
        v.append(a[i - 1] - a[i])
         
    # Sort vector to find minimum k
    v.sort()
     
    # Initialize result
    res = a[n - 1] - a[0]
     
    # Adding first k-1 values
    for i in range(k - 1):
        res += v[i]
     
    # Return the minimized sum
    return res
     
# Driver code
arr = [ 4, 8, 15, 16, 23, 42 ]
 
# Length of array
N = len(arr)
 
K = 3
 
# Function Call
print(find(arr, N, K))
 
# This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the subarray
static int find(int []a, int n, int k)
{
    List v = new List();
 
    // Add the difference to vectors
    for(int i = 1; i < n; ++i)
    {
       v.Add(a[i - 1] - a[i]);
    }
 
    // Sort vector to find minimum k
    v.Sort();
 
    // Initialize result
    int res = a[n - 1] - a[0];
 
    // Adding first k-1 values
    for(int i = 0; i < k - 1; ++i)
    {
       res += v[i];
    }
     
    // Return the minimized sum
    return res;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given array []arr
    int []arr = { 4, 8, 15, 16, 23, 42 };
    int N = arr.Length;
     
    // Given K
    int K = 3;
     
    // Function Call
    Console.Write(find(arr, N, K) + "\n");
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
12

时间复杂度: O(N) ,其中 N 是数组中的元素数。
辅助空间: O(N) ,其中 N 是数组中元素的数量。