📌  相关文章
📜  根据给定条件从数组中删除元素的最大分数

📅  最后修改于: 2021-05-19 17:25:54             🧑  作者: Mango

给定一个数组arr [] ,任务是找到删除元素的最大分数,其中可以用该元素的分数删除该数组的每个元素,但是约束是如果我们删除arr [i] ,则arr [ i] + 1arr [i] – 1被自动删除,得分为0。
例子:

方法:想法是使用动态编程来解决此问题。该问题的主要观察结果是,对于从数组中删除任何元素,元素的出现和值本身是重要的因素。
让我们举一个例子来了解序列是4 4 5的情况。然后,我们有两个选择,从4到5。现在,选择4时,他的分数将是4 * 2 = 8。如果我们选择5,他的分数将是5 * 1 =5。显然,最大分数是8。
因此,对于上述序列4 4 5,freq [4] = 2,freq [5] = 1。
最后,要找到最佳分数,首先将问题分解为较小的问题将很容易。在这种情况下,我们将序列分成较小的序列,并找到最佳的解决方案。对于仅包含0的数字序列,答案将为0。类似地,如果序列仅包含数字0和1,则解决方案将为count [1] * 1。
递归关系:

基本上,我们有2种情况,要么选择i元素,要么不选择i元素。

情况1:如果我们选择第ith个元素,则直到第ith个元素的最大分数将为dp [i-2] + i * freq [i](选择ith元素意味着删除第(i-1)个元素)
情况2:如果我们不选择第ith个元素,则直到第ith个元素的最大得分将为dp [i-1]
现在,由于我们必须最大化分数,因此我们将两者都取最大值。
下面是上述方法的实现:

C++
// C++ implementation to find the
// maximum score of the deleting a
// element from an array
  
#include 
  
using namespace std;
  
// Function to find the maximum
// score of the deleting an element
// from an array
int findMaximumScore(vector a, int n)
{
  
    // Creating a map to keep
    // the frequency of numbers
    unordered_map freq;
  
    // Loop to iterate over the
    // elements of the array
    for (int i = 0; i < n; i++) {
        freq[a[i]]++;
    }
  
    // Creating a DP array to keep
    // count of max score at ith element
    // and it will be filled
    // in the bottom Up manner
    vector dp(*max_element(a.begin(),
                                a.end())
                       + 1,
                   0);
    dp[0] = 0;
    dp[1] = freq[1];
  
    // Loop to choose the elements of the
    // array to delete from the array
    for (int i = 2; i < dp.size(); i++)
        dp[i] = max(
            dp[i - 1],
            dp[i - 2] + freq[i] * i);
  
    return dp[dp.size() - 1];
}
  
// Driver Code
int main()
{
    int n;
    n = 3;
    vector a{ 1, 2, 3 };
  
    // Function Call
    cout << findMaximumScore(a, n);
    return 0;
}


Java
// Java implementation to find the
// maximum score of the deleting a
// element from an array
import java.util.*;
  
class GFG{
  
// Function to find the maximum
// score of the deleting an element
// from an array
static int findMaximumScore(int []a, int n)
{
      
    // Creating a map to keep
    // the frequency of numbers
    @SuppressWarnings("unchecked")
    HashMap freq = new HashMap();
  
    // Loop to iterate over the
    // elements of the array
    for(int i = 0; i < n; i++) 
    {
        if(freq.containsKey(a[i]))
        {
            freq.put(a[i], 
                     freq.get(a[i]) + 1);
        }
        else
        {
            freq.put(a[i], 1);
        }
    }
  
    // Creating a DP array to keep
    // count of max score at ith element
    // and it will be filled
    // in the bottom Up manner
    int []dp = new int[Arrays.stream(a).max().getAsInt() + 1];
    dp[0] = 0;
    dp[1] = freq.get(1);
  
    // Loop to choose the elements of the
    // array to delete from the array
    for(int i = 2; i < dp.length; i++)
        dp[i] = Math.max(dp[i - 1],
                         dp[i - 2] + 
                       freq.get(i) * i);
  
    return dp[dp.length - 1];
}
  
// Driver Code
public static void main(String[] args)
{
    int n;
    n = 3;
    int []a = { 1, 2, 3 };
  
    // Function call
    System.out.print(findMaximumScore(a, n));
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation to find the
# maximum score of the deleting a
# element from an array
from collections import defaultdict
  
# Function to find the maximum
# score of the deleting an element
# from an array
def findMaximumScore(a, n):
    
    # Creating a map to keep
    # the frequency of numbers
    freq = defaultdict (int)
  
    # Loop to iterate over the
    # elements of the array
    for i in range (n):
        freq[a[i]] += 1
  
    # Creating a DP array to keep
    # count of max score at ith element
    # and it will be filled
    # in the bottom Up manner
    dp = [0] * (max(a) + 1)
    dp[0] = 0
    dp[1] = freq[1]
  
    # Loop to choose the elements of the
    # array to delete from the array
    for i in range (2, len(dp)):
        dp[i] = max(dp[i - 1], 
                    dp[i - 2] + 
                    freq[i] * i)
  
    return dp[- 1]
  
# Driver Code
if __name__ == "__main__":
    
    n = 3
    a = [1, 2, 3]
  
    # Function Call
    print(findMaximumScore(a, n)) 
  
# This code is contributed by Chitranayal


C#
// C# implementation to find the
// maximum score of the deleting a
// element from an array
using System;
using System.Linq;
using System.Collections.Generic;
  
class GFG{
  
// Function to find the maximum
// score of the deleting an element
// from an array
static int findMaximumScore(int []a, int n)
{
      
    // Creating a map to keep
    // the frequency of numbers
    Dictionary freq = new Dictionary();
  
    // Loop to iterate over the
    // elements of the array
    for(int i = 0; i < n; i++) 
    {
        if(freq.ContainsKey(a[i]))
        {
            freq[a[i]] = freq[a[i]] + 1;
        }
        else
        {
            freq.Add(a[i], 1);
        }
    }
  
    // Creating a DP array to keep
    // count of max score at ith element
    // and it will be filled
    // in the bottom Up manner
    int []dp = new int[a.Max() + 1];
    dp[0] = 0;
    dp[1] = freq[1];
  
    // Loop to choose the elements of the
    // array to delete from the array
    for(int i = 2; i < dp.Length; i++)
        dp[i] = Math.Max(dp[i - 1],
                         dp[i - 2] + 
                       freq[i] * i);
  
    return dp[dp.Length - 1];
}
  
// Driver Code
public static void Main(String[] args)
{
    int n;
    n = 3;
    int []a = { 1, 2, 3 };
      
    // Function call
    Console.Write(findMaximumScore(a, n));
}
}
  
// This code is contributed by 29AjayKumar


输出:
4