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

📅  最后修改于: 2021-09-06 06:33:09             🧑  作者: 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。
复发关系:

基本上,我们有两种情况,要么选择i元素,另一种是不选择i元素。

情况1:如果我们选择第i个元素,直到第i个元素的最大分数将是dp[i-2] + i*freq[i](选择第i个元素意味着删除第(i-1)个元素)
情况 2:如果我们不选择第 i 个元素,则直到第 i 个元素的最大分数将为 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



如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live