给定一个由N 个整数和一个整数K组成的数组arr[] ,任务是将给定的数组拆分为K 个不重叠的子集,使得所有子集之和中的最大值最小。
例子:
Input: arr[] = {1, 7, 9, 2, 12, 3, 3}, M = 3
Output: 13
Explanation:
One possible way to spit the array into 3 non-overlapping subsets is {arr[4], arr[0]}, {arr[2], arr[6]}, and {arr[1], arr[5], arr[3]}.
The sum of each subset is 13, 12 and 12 respectively. Now, the maximum among all the sum of subsets is 13, which is the minimum possible sum.
Input: arr[] = {1, 2, 3, 4, 5}, M = 2
Output: 8
方法:通过使用优先级队列并对给定数组进行排序,可以通过贪婪方法解决给定的问题。 请按照以下步骤解决问题:
- 使用priority_queue 初始化Min-Heap 说PQ来存储每组元素的总和。
- 在[1, K]范围内迭代并将0推入PQ 。
- 按降序对数组arr[]进行排序。
- 遍历数组ARR []以及对于每个阵列元件的常用3 [I],元素ARR [I]添加到将在优先级队列PQ的顶部的最小和基团。
- 完成上述步骤后,打印优先队列PQ的最后一个元素作为所有可能组中最小的最大和。
下面是上述方法的实现:
+++++
C++
// C++ program for the above approach
#include
using namespace std;
// Function to split the array into M
// groups such that maximum of the sum
// of all elements of all the groups
// is minimized
int findMinimumValue(int arr[], int N,
int M)
{
// Sort the array in decreasing order
sort(arr, arr + N, greater());
// Initialize priority queue (Min heap)
priority_queue,
greater >
pq;
// Push 0 for all the M groups
for (int i = 1; i <= M; ++i) {
pq.push(0);
}
// Traverse the array, arr[]
for (int i = 0; i < N; ++i) {
// Pop the group having the
// minimum sum
int val = pq.top();
pq.pop();
// Increment val by arr[i]
val += arr[i];
// Push the new sum of the
// group into the pq
pq.push(val);
}
// Iterate while size of the pq
// is greater than 1
while (pq.size() > 1) {
pq.pop();
}
// Return result
return pq.top();
}
// Driver Code
int main()
{
int arr[] = { 1, 7, 9, 2, 12, 3, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 3;
cout << findMinimumValue(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class Main
{
// Function to split the array into M
// groups such that maximum of the sum
// of all elements of all the groups
// is minimized
static int findMinimumValue(Vector arr, int N, int M)
{
// Sort the array in decreasing order
Collections.sort(arr);
Collections.reverse(arr);
// Initialize priority queue (Min heap)
Vector pq = new Vector();
// Push 0 for all the M groups
for (int i = 1; i <= M; ++i) {
pq.add(0);
}
Collections.sort(pq);
// Traverse the array, arr[]
for (int i = 0; i < N; ++i) {
// Pop the group having the
// minimum sum
int val = pq.get(0);
pq.remove(0);
// Increment val by arr[i]
val += arr.get(i);
// Push the new sum of the
// group into the pq
pq.add(val);
Collections.sort(pq);
}
// Iterate while size of the pq
// is greater than 1
while (pq.size() > 1) {
pq.remove(0);
}
// Return result
return pq.get(0);
}
public static void main(String[] args) {
Integer[] arr = { 1, 7, 9, 2, 12, 3, 3 };
Vector Arr = new Vector();
Collections.addAll(Arr, arr);
int N = Arr.size();
int K = 3;
System.out.println(findMinimumValue(Arr, N, K));
}
}
// This code is contributed by divyesh072019.
Python3
# Python3 program for the above approach
# Function to split the array into M
# groups such that maximum of the sum
# of all elements of all the groups
# is minimized
def findMinimumValue(arr, N, M):
# Sort the array in decreasing order
arr.sort()
arr.reverse()
# Initialize priority queue (Min heap)
pq = []
# Push 0 for all the M groups
for i in range(1, M + 1):
pq.append(0)
pq.sort()
# Traverse the array, arr[]
for i in range(N):
# Pop the group having the
# minimum sum
val = pq[0]
del pq[0]
# Increment val by arr[i]
val += arr[i]
# Push the new sum of the
# group into the pq
pq.append(val)
pq.sort()
# Iterate while size of the pq
# is greater than 1
while (len(pq) > 1) :
del pq[0]
# Return result
return pq[0]
arr = [ 1, 7, 9, 2, 12, 3, 3 ]
N = len(arr)
K = 3
print(findMinimumValue(arr, N, K))
# This code is contributed by suresh07.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to split the array into M
// groups such that maximum of the sum
// of all elements of all the groups
// is minimized
static int findMinimumValue(int[] arr, int N, int M)
{
// Sort the array in decreasing order
Array.Sort(arr);
Array.Reverse(arr);
// Initialize priority queue (Min heap)
List pq = new List();
// Push 0 for all the M groups
for (int i = 1; i <= M; ++i) {
pq.Add(0);
}
pq.Sort();
// Traverse the array, arr[]
for (int i = 0; i < N; ++i) {
// Pop the group having the
// minimum sum
int val = pq[0];
pq.RemoveAt(0);
// Increment val by arr[i]
val += arr[i];
// Push the new sum of the
// group into the pq
pq.Add(val);
pq.Sort();
}
// Iterate while size of the pq
// is greater than 1
while (pq.Count > 1) {
pq.RemoveAt(0);
}
// Return result
return pq[0];
}
static void Main() {
int[] arr = { 1, 7, 9, 2, 12, 3, 3 };
int N = arr.Length;
int K = 3;
Console.Write(findMinimumValue(arr, N, K));
}
}
// This code is contributed by divyeshrabadiya07.
Javascript
输出:
13
时间复杂度: O(N*log K)
辅助空间: O(M)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。