给定一个整数K和一个长度为K倍数的数组A[ ] ,任务是将给定数组的元素拆分为K个子集,每个子集具有相同数量的元素,使得最大和最小元素的总和每个子集的最大总和是可能的。
例子:
Input: K = 2, A[ ] = {1, 13, 7, 17, 6, 5}
Output: 37
Explanation:
1st group: {1, 5, 17} maximum = 17, minimum = 1
2nd group: {6, 7, 13} maximum = 13, minimum = 6
Hence, maximum possible sum = 17 + 1 + 13 + 6 = 37
Input: K = 2, A[ ] = {10, 10, 10, 10, 11, 11}
Output: 42
Explanation:
1st group: {11, 10, 10} maximum = 11, minimum = 10
2nd group: {11, 10, 10} maximum = 11, minimum = 10
Hence, maximum sum possible = 11 + 10 + 11 + 10 = 42
天真的方法:
解决这个问题的最简单方法是生成大小为N/K的K个子集的所有可能组,并为每个组找出每个子集中的最大值和最小值并计算它们的总和。计算出所有组的总和后,打印获得的最大总和。
时间复杂度: O(2 N )
辅助空间: O(N)
有效的方法:
这个想法是使用贪婪技术优化上述方法。由于需要每个子集的最大和最小元素的最大和,所以尽量最大化最大元素和最小元素。对于每个子集的最大元素,从给定数组中取出前K 个最大元素并将每个元素插入到不同的子集中。对于每个子集的最小元素,从排序数组中,从索引0开始,在(N / K) – 1间隔处选择每个下一个元素,因为每个子集的大小是N / K并且每个子集已经包含一个最大元素。
请按照以下步骤操作:
- 计算每组中的元素数,即(N/K) 。
- 按非降序对A[ ] 的所有元素进行排序。
- 对于最大元素的总和,将排序数组中的所有K 个最大元素相加。
- 对于最小元素的总和,从索引0开始,选择每个具有(N / K) – 1间隔的K 个元素并添加它们。
- 最后,计算最大元素的总和和最小元素的总和。打印它们各自总和的总和作为最终答案。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function that prints
// the maximum sum possible
void maximumSum(int arr[],
int n, int k)
{
// Find elements in each group
int elt = n / k;
int sum = 0;
// Sort all elements in
// non-descending order
sort(arr, arr + n);
int count = 0;
int i = n - 1;
// Add K largest elements
while (count < k) {
sum += arr[i];
i--;
count++;
}
count = 0;
i = 0;
// For sum of minimum
// elements from each subset
while (count < k) {
sum += arr[i];
i += elt - 1;
count++;
}
// Printing the maximum sum
cout << sum << "\n";
}
// Driver Code
int main()
{
int Arr[] = { 1, 13, 7, 17, 6, 5 };
int K = 2;
int size = sizeof(Arr) / sizeof(Arr[0]);
maximumSum(Arr, size, K);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.Arrays;
class GFG{
// Function that prints
// the maximum sum possible
static void maximumSum(int arr[],
int n, int k)
{
// Find elements in each group
int elt = n / k;
int sum = 0;
// Sort all elements in
// non-descending order
Arrays.sort(arr);
int count = 0;
int i = n - 1;
// Add K largest elements
while (count < k)
{
sum += arr[i];
i--;
count++;
}
count = 0;
i = 0;
// For sum of minimum
// elements from each subset
while (count < k)
{
sum += arr[i];
i += elt - 1;
count++;
}
// Printing the maximum sum
System.out.println(sum);
}
// Driver code
public static void main (String[] args)
{
int Arr[] = { 1, 13, 7, 17, 6, 5 };
int K = 2;
int size = Arr.length;
maximumSum(Arr, size, K);
}
}
// This code is contributed by Shubham Prakash
Python3
# Python3 program to implement
# the above approach
# Function that prints
# the maximum sum possible
def maximumSum(arr, n, k):
# Find elements in each group
elt = n // k;
sum = 0;
# Sort all elements in
# non-descending order
arr.sort();
count = 0;
i = n - 1;
# Add K largest elements
while (count < k):
sum += arr[i];
i -= 1;
count += 1;
count = 0;
i = 0;
# For sum of minimum
# elements from each subset
while (count < k):
sum += arr[i];
i += elt - 1;
count += 1;
# Printing the maximum sum
print(sum);
# Driver code
if __name__ == '__main__':
Arr = [1, 13, 7, 17, 6, 5];
K = 2;
size = len(Arr);
maximumSum(Arr, size, K);
# This code is contributed by sapnasingh4991
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function that prints
// the maximum sum possible
static void maximumSum(int []arr,
int n, int k)
{
// Find elements in each group
int elt = n / k;
int sum = 0;
// Sort all elements in
// non-descending order
Array.Sort(arr);
int count = 0;
int i = n - 1;
// Add K largest elements
while (count < k)
{
sum += arr[i];
i--;
count++;
}
count = 0;
i = 0;
// For sum of minimum
// elements from each subset
while (count < k)
{
sum += arr[i];
i += elt - 1;
count++;
}
// Printing the maximum sum
Console.WriteLine(sum);
}
// Driver code
public static void Main(String[] args)
{
int []Arr = { 1, 13, 7, 17, 6, 5 };
int K = 2;
int size = Arr.Length;
maximumSum(Arr, size, K);
}
}
// This code is contributed by amal kumar choubey
Javascript
37
时间复杂度: O(N*logN)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live