📌  相关文章
📜  最大化数组中所选数字的总和以清空它 |设置 2

📅  最后修改于: 2022-05-13 01:56:04.292000             🧑  作者: Mango

最大化数组中所选数字的总和以清空它 |设置 2

给定一个包含N个整数的数组arr[] ,任务是在所有操作中最大化所选数字的总和,使得在每个操作中,选择一个数字A i ,删除它的一次出现并删除所有出现的A i – 1A i + 1 (如果它们存在)在数组中,直到数组变空。

例子:

方法:给定的问题可以通过计算数组元素的频率来解决,然后找到本文上一篇文章中讨论的最大和。

时间复杂度: O(M + F),其中M是数组的最大元素,F 是数组元素的最大频率。
辅助空间: O(M),其中M是数组的最大元素。

动态规划方法:上述方法也可以使用动态规划进行优化和解决。可以观察到,如果选择数组arr[]的一个数A i ,它将把A i * freq[A i ]贡献到最终总和中。使用此观察结果,请按照以下步骤解决给定问题:

  • 创建一个数组freq[] ,它将每个元素的频率存储在数组arr[]中。
  • 创建一个一维数组dp[] ,其中dp[i]表示给定数组中位于[1, i]范围内的选定值的最大可能总和。
  • 对于i的每个值,有两种可能的情况如下:
    • 案例 1,其中i被选中。在这种情况下, dp[i] = freq[i] * i + dp[i-2]的值。
    • 案例 2,其中i – 1被选中。在这种情况下, dp[i] = dp[i-1]的值。
  • 因此,上述问题的DP关系为:
  • 存储在dp[MAX]中的值是所需的答案。

下面是上述方法的实现:

C++
// cpp program for the above approach
#include 
using namespace std;
 
// Function to find the maximum sum of
// selected numbers from an array to make
// the array empty
int maximizeSum(vector arr)
{
 
    // Edge Case
    if (arr.size() == 0)
        return 0;
 
    // Stores the frequency of each element
    // in the range [0, MAX] where MAX is
    // the maximum integer in the array arr
 
    int mx= *max_element(arr.begin(),arr.end());
    int freq[mx + 1]={0};
 
    // Loop to iterate over array arr[]
    for (int i : arr)
        freq[i] += 1;
 
    // Stores the DP states
    int dp[mx + 1]={0};
 
    // Initially dp[1] = freq[1]
    dp[1] = freq[1];
 
    // Iterate over the range [2, MAX]
    for (int i = 2; i < mx + 1; i++)
        dp[i] = max(freq[i] * i + dp[i - 2],
                    dp[i - 1]);
 
    // Return Answer
    return dp[mx];
}
 
// Driver Code
int main()
{
    vector arr = {2, 2, 3, 3, 3, 4};
 
    // Function Call
    cout << (maximizeSum(arr));
}
 
// This code is contributed by amreshkumar3.


Java
// java program for the above approach
class GFG
{
   
    // Utility function to find
    // maximum value of an element in array
    static int getMax(int [] arr)
    {
        int max = Integer.MIN_VALUE;
       
          for(int i = 0; i < arr.length; i++)
        {
           if(arr[i] > max)
             max = arr[i];
        }
       
          return max;
       
    }
   
    // Function to find the maximum sum of
    // selected numbers from an array to make
    // the array empty
    static int maximizeSum(int [] arr)
    {
 
        // Edge Case
        if (arr.length == 0)
            return 0;
 
        // Stores the frequency of each element
        // in the range [0, MAX] where MAX is
        // the maximum integer in the array arr
     
        int max = getMax(arr);
        int [] freq = new int[max + 1];
 
        // Loop to iterate over array arr[]
        for (int i : arr)
          freq[i] += 1;
 
        // Stores the DP states
        int[] dp = new int[max + 1];
 
        // Initially dp[1] = freq[1]
        dp[1] = freq[1];
 
        // Iterate over the range [2, MAX]
        for (int i = 2; i < max + 1; i++)
            dp[i] = Math.max(freq[i] * i + dp[i - 2],
                             dp[i - 1]);
 
        // Return Answer
        return dp[max];
    }
 
    // Driver Code
    public static void main(String [] args)
    {
        int [] arr = { 2, 2, 3, 3, 3, 4 };
 
        // Function Call
        System.out.println((maximizeSum(arr)));
    }
}
 
// This code is contributed by AR_Gaurav


Python3
# Python program for the above approach
 
# Function to find the maximum sum of
# selected numbers from an array to make
# the array empty
def maximizeSum(arr):
 
    # Edge Case
    if not arr:
        return 0
 
    # Stores the frequency of each element
    # in the range [0, MAX] where MAX is
    # the maximum integer in the array arr
    freq = [0] * (max(arr)+1)
 
    # Loop to iterate over array arr[]
    for i in arr:
        freq[i] += 1
 
    # Stores the DP states
    dp = [0] * (max(arr)+1)
 
    # Initially dp[1] = freq[1]
    dp[1] = freq[1]
 
    # Iterate over the range [2, MAX]
    for i in range(2, max(arr)+1):
        dp[i] = max(freq[i]*i + dp[i-2], dp[i-1])
 
    # Return Answer
    return dp[max(arr)]
 
# Driver Code
arr = [2, 2, 3, 3, 3, 4]
 
# Function Call
print(maximizeSum(arr))


C#
// C# program for the above approach
using System;
using System.Linq;
class GFG
{
   
    // Function to find the maximum sum of
    // selected numbers from an array to make
    // the array empty
    static int maximizeSum(int[] arr)
    {
 
        // Edge Case
        if (arr.Length == 0)
            return 0;
 
        // Stores the frequency of each element
        // in the range [0, MAX] where MAX is
        // the maximum integer in the array arr
        int[] freq = new int[(arr.Max() + 1)];
 
        // Loop to iterate over array arr[]
        foreach(int i in arr) freq[i] += 1;
 
        // Stores the DP states
        int[] dp = new int[(arr.Max() + 1)];
 
        // Initially dp[1] = freq[1]
        dp[1] = freq[1];
 
        // Iterate over the range [2, MAX]
        for (int i = 2; i < arr.Max() + 1; i++)
            dp[i] = Math.Max(freq[i] * i + dp[i - 2],
                             dp[i - 1]);
 
        // Return Answer
        return dp[arr.Max()];
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 2, 2, 3, 3, 3, 4 };
 
        // Function Call
        Console.WriteLine((maximizeSum(arr)));
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
9

时间复杂度: O(M + F),其中M数组的最大元素
辅助空间: O(M),其中M数组的最大元素