所有子集的最大元素之和
给定一个整数数组,我们需要找到所有可能子集的最大数量之和。
例子:
Input : arr = {3, 2, 5}
Output : 28
Explanation :
Subsets and their maximum are,
{} maximum = 0
{3} maximum = 3
{2} maximum = 2
{5} maximum = 5
{3, 2} maximum = 3
{3, 5} maximum = 5
{2, 5} maximum = 5
{3, 2, 5} maximum = 5
Sum of maximum will be, 0 + 3 + 2 + 5 + 3 + 5 + 5 + 5 = 28,
which will be our answer.
一个简单的解决方案是遍历数组的所有子集并找到所有子集的最大值,然后将它们添加到我们的答案中,但这种方法将导致我们的时间复杂度呈指数增长。
一个有效的解决方案基于一件事,即有多少数组子集以特定元素为最大值。如上例所示,四个子集的最大值为 5,两个子集的最大值为 3,一个子集的最大值为 2。这个想法是计算与数组的每个元素相对应的这些频率。一旦我们有了频率,我们就可以将它们与数组值相乘并将它们全部相加,这将导致我们的最终结果。
为了找到频率,首先我们以非递增顺序对数组进行排序,当我们站在 a[i] 处时,我们知道,从 a[i + 1] 到 a[N-1] 的所有元素都小于 a[i] ,因此由这些元素生成的任何子集都将选择 a[i] 作为其最大值,因此对应于 a[i] 的此类子集的计数将为 2^(N – i – 1)(由 a[ 中的数组元素生成的总子集i + 1] 到 a[N])。如果对数组的所有元素应用相同的过程,我们将得到最终答案,
res = a[0]*2^(N-1) + a[1]*2^(N-2) ..... + a[i]*2^(Ni-1) + ..... + a[ N-1]*2^(0)
现在如果我们按原样求解上述方程,计算 2 的幂在每个指标上都需要时间,相反我们可以将方程改成类似于 horner 规则的简化,
res = a[N] + 2*(a[N-1] + 2*(a[N-2] + 2*( …… 2*(a[2] + 2*a[1])….. ))))
上述解决方案的总复杂度为 O(N*log(N))
C++
// C/C++ code to find sum of maximum of all subsets of array
#include
using namespace std;
// Method returns sum of maximum of all subsets
int sumOfMaximumOfSubsets(int arr[], int N)
{
// sorting array in decreasing order
sort(arr, arr + N, greater());
// initializing sum with first element
int sum = arr[0];
for (int i = 1; i < N; i++)
{
// calculating evaluation similar to horner's rule
sum = 2 * sum + arr[i];
}
return sum;
}
// Driver code to test above methods
int main()
{
int arr[] = {3, 2, 5};
int N = sizeof(arr) / sizeof(arr[0]);
cout << sumOfMaximumOfSubsets(arr, N) << endl;
return 0;
}
Java
import java.util.Arrays;
import java.util.Collections;
// Java code to find sum of
// maximum of all subsets of array
class GFG
{
// Method returns sum of maximum of all subsets
static int sumOfMaximumOfSubsets(Integer arr[], int N)
{
// sorting array in decreasing order
Arrays.sort(arr, Collections.reverseOrder());
// initializing sum with first element
int sum = arr[0];
for (int i = 1; i < N; i++)
{
// calculating evaluation similar to horner's rule
sum = 2 * sum + arr[i];
}
return sum;
}
// Driver code
public static void main(String[] args)
{
Integer arr[] = {3, 2, 5};
int N = arr.length;
System.out.println(sumOfMaximumOfSubsets(arr, N));
}
}
/* This code contributed by PrinciRaj1992 */
Python3
# Python 3 code to find sum
# of maximum of all subsets
# of array
# Method returns sum of
# maximum of all subsets
def sumOfMaximumOfSubsets(arr, N):
# sorting array in
# decreasing order
arr.sort(reverse = True)
# initializing sum
# with first element
sum = arr[0]
for i in range(1, N):
# calculating evaluation
# similar to horner's rule
sum = 2 * sum + arr[i]
return sum
# Driver code
arr = [3, 2, 5]
N = len(arr)
print(sumOfMaximumOfSubsets(arr, N))
# This code is contributed
# by Smitha
C#
// C# code to find sum of
// maximum of all subsets of array
using System;
class GFG
{
// Method returns sum of maximum of all subsets
static int sumOfMaximumOfSubsets(int []arr, int N)
{
// sorting array in decreasing order
Array.Sort(arr);
Array.Reverse(arr);
// initializing sum with first element
int sum = arr[0];
for (int i = 1; i < N; i++)
{
// calculating evaluation
// similar to horner's rule
sum = 2 * sum + arr[i];
}
return sum;
}
// Driver code
public static void Main(String[] args)
{
int []arr = {3, 2, 5};
int N = arr.Length;
Console.WriteLine(sumOfMaximumOfSubsets(arr, N));
}
}
// This code has been contributed by 29AjayKumar
PHP
Javascript
输出:
28