📌  相关文章
📜  删除所有元素的所有出现以获取最大数组和

📅  最后修改于: 2021-04-21 21:55:59             🧑  作者: Mango

给定一个正整数数组,请删除所有出现的element以获得剩余数组的最大和。

例子:

蛮力解决方案是先找到数组的和,再找到数组中元素的所有频率。找到他们贡献给数组总和的值。在其中选择最小值。去除后获得数组的最大和等于单个元素总频繁值贡献的总和与最小值之差。

时间复杂度: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


输出:
11

时间复杂度: O(n)