给定一个正整数数组,删除所有出现的元素以获得剩余数组的最大和。
例子:
Input : arr = {1, 1, 3}
Output : 3
On removing 1 from the array, we get {3}. The total value is 3
Input : arr = {1, 1, 3, 3, 2, 2, 1, 1, 1}
Output : 11
On removing 2 from the array, we get {1, 1, 3, 3, 1, 1, 1}. The total value is 11.
Brute Force 解决方案是先求一个数组的和,然后再求该数组中元素的所有频率。找出它们对数组总和的贡献。选择其中的最小值。去除后得到数组的最大和是单个元素的总频繁值所贡献的和的总值与最小值的等差。
时间复杂度: O(n 2 )
一个更好的方法我们首先找到数组的总和,然后对数组进行排序,在遍历数组的同时计算各个频率并获得最大值。排序后,我们可以使用 O(n) 中所有元素的频率,
这种方法的时间复杂度是 O(n Log n)
一种有效的方法是使用散列来计算遍历数组时元素的频率。使用存储在数组中的频率找到最小值
C++
#include
using namespace std;
int maxSumArray(int arr[], int n)
{
// Find total sum and frequencies of elements
int sum = 0;
unordered_map mp;
for (int i = 0; i < n; i++) {
sum += arr[i];
mp[arr[i]]++;
}
// Find minimum value to be subtracted.
int minimum = INT_MAX;
for (auto x : mp)
minimum = min(minimum, x.second * x.first);
// Find maximum sum after removal
return (sum - minimum);
}
// Drivers code
int main()
{
int arr[] = { 1, 1, 3, 3, 2, 2, 1, 1, 1 };
int n = sizeof(arr) / sizeof(int);
cout << maxSumArray(arr, n);
return 0;
}
Java
// Java program to convert fractional decimal
// to binary number
import java.util.*;
class GFG
{
static int maxSumArray(int arr[], int n)
{
// Find total sum and frequencies of elements
int sum = 0;
Map m = new HashMap<>();
for (int i = 0 ; i < n; i++)
{
sum += arr[i];
if(m.containsKey(arr[i]))
{
m.put(arr[i], m.get(arr[i])+1);
}
else
{
m.put(arr[i], 1);
}
}
// Find minimum value to be subtracted.
int minimum = Integer.MAX_VALUE;
for (Map.Entry x : m.entrySet())
minimum = Math.min(minimum, x.getValue() * x.getKey());
// Find maximum sum after removal
return (sum - minimum);
}
// Drivers code
public static void main(String[] args)
{
int arr[] = { 1, 1, 3, 3, 2, 2, 1, 1, 1 };
int n = arr.length;
System.out.println(maxSumArray(arr, n));
}
}
// This code contributed by Rajput-Ji
Python3
# Python3 program to convert
# fractional decimal to binary number
from sys import maxsize
def maxSumArray(arr, n):
# Find total sum and frequencies of elements
sum1 = 0
mp = {i:0 for i in range(4)}
for i in range(n):
sum1 += arr[i]
mp[arr[i]] += 1
# Find minimum value to be subtracted.
minimum = maxsize
for key, value in mp.items():
if(key == 0):
continue
minimum = min(minimum, value * key)
# Find maximum sum after removal
return (sum1 - minimum)
# Driver Code
if __name__ =='__main__':
arr = [1, 1, 3, 3, 2, 2, 1, 1, 1]
n = len(arr)
print(maxSumArray(arr, n))
# This code is contributed by
# Surendra_Gangwar
C#
// C# program to convert fractional decimal
// to binary number
using System;
using System.Collections.Generic;
class GFG
{
static int maxSumArray(int []arr, int n)
{
// Find total sum and frequencies of elements
int sum = 0;
Dictionary m = new Dictionary();
for (int i = 0 ; i < n; i++)
{
sum += arr[i];
if(m.ContainsKey(arr[i]))
{
var val = m[arr[i]];
m.Remove(arr[i]);
m.Add(arr[i], val + 1);
}
else
{
m.Add(arr[i], 1);
}
}
// Find minimum value to be subtracted.
int minimum = int.MaxValue;
foreach(KeyValuePair x in m)
minimum = Math.Min(minimum, (x.Value * x.Key));
// Find maximum sum after removal
return (sum - minimum);
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 1, 1, 3, 3, 2, 2, 1, 1, 1 };
int n = arr.Length;
Console.WriteLine(maxSumArray(arr, n));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
11
时间复杂度: O(n)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。