📌  相关文章
📜  具有最大不同元素的子序列数

📅  最后修改于: 2021-06-25 15:25:19             🧑  作者: Mango

给定一个大小为narr 。问题是要计算具有最大数量的不同元素的所有子序列。

例子:

Input : arr[] = {4, 7, 6, 7}
Output : 2
The indexes for the subsequences are:
{0, 1, 2} - Subsequence is {4, 7, 6} and
{0, 2, 3} - Subsequence is {4, 6, 7}

Input : arr[] = {9, 6, 4, 4, 5, 9, 6, 1, 2}
Output : 8

天真的方法:考虑所有具有不同元素的子序列,并计算具有最大不同元素的子序列。

高效的方法:创建一个哈希表来存储数组中每个元素的频率。取所有频率的乘积。
该解决方案基于以下事实:当所有元素都不同时,总有1个子序列。如果元素重复,则每次出现重复元素都会构成一系列不同元素的子序列。

C++
// C++ implementation to count subsequences having
// maximum distinct elements
#include 
using namespace std;
  
typedef unsigned long long int ull;
  
// function to count subsequences having
// maximum distinct elements
ull countSubseq(int arr[], int n)
{
    // unordered_map 'um' implemented as
    // hash table
    unordered_map um;
  
    ull count = 1;
  
    // count frequency of each element
    for (int i = 0; i < n; i++)
        um[arr[i]]++;
  
    // traverse 'um'
    for (auto itr = um.begin(); itr != um.end(); itr++)
  
        // multiply frequency of each element
        // and accumulate it in 'count'
        count *= (itr->second);
  
    // required number of subsequences
    return count;
}
  
// Driver program to test above
int main()
{
    int arr[] = { 4, 7, 6, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Count = "
         << countSubseq(arr, n);
    return 0;
}


Java
// Java implementation to count subsequences having 
// maximum distinct elements
import java.util.HashMap;
  
class geeks
{
      
    // function to count subsequences having
    // maximum distinct elements
    public static long countSubseq(int[] arr, int n) 
    {
  
        // unordered_map 'um' implemented as
        // hash table
        HashMap um = new HashMap<>();
  
        long count = 1;
  
        // count frequency of each element
        for (int i = 0; i < n; i++)
        {
            if (um.get(arr[i]) != null)
            {
                int a = um.get(arr[i]);
                um.put(arr[i], ++a);
            }
            else
                um.put(arr[i], 1);
        }
          
        // traverse 'um'
        for (HashMap.Entry entry : um.entrySet())
        {
              
            // multiply frequency of each element
            // and accumulate it in 'count'
            count *= entry.getValue();
        }
          
        // required number of subsequences
        return count;
    }
  
    // Driver Code
    public static void main(String[] args) 
    {
        int[] arr = { 4, 7, 6, 7 };
        int n = arr.length;
        System.out.println("Count = " + countSubseq(arr, n));
    }
}
  
// This code is contributed by
// sanjeev2552


Python3
# Python 3 implementation to count subsequences 
# having maximum distinct elements
  
# function to count subsequences having
# maximum distinct elements
def countSubseq(arr, n):
      
    # unordered_map 'um' implemented
    # as hash table
    # take range equal to maximum 
    # value of arr
    um = {i:0 for i in range(8)}
  
    count = 1
  
    # count frequency of each element
    for i in range(n):
        um[arr[i]] += 1
  
    # traverse 'um'
    for key, values in um.items():
          
        # multiply frequency of each element
        # and accumulate it in 'count'
        if(values > 0):
            count *= values
  
    # required number of subsequences
    return count
  
# Driver Code
if __name__ == '__main__':
    arr = [4, 7, 6, 7]
    n = len(arr)
    print("Count =", countSubseq(arr, n))
  
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation to count subsequences
// having maximum distinct elements
using System;
using System.Collections.Generic; 
      
class GFG
{
      
    // function to count subsequences having
    // maximum distinct elements
    public static long countSubseq(int[] arr,
                                   int n) 
    {
  
        // unordered_map 'um' implemented as
        // hash table
        Dictionary um = new Dictionary();
  
        long count = 1;
  
        // count frequency of each element
        for (int i = 0; i < n; i++)
        {
            if (um.ContainsKey(arr[i]))
            {
                int a = um[arr[i]];
                um.Remove(arr[i]);
                um.Add(arr[i], ++a);
            }
            else
                um.Add(arr[i], 1);
        }
          
        // traverse 'um'
        foreach(KeyValuePair entry in um)
        {
              
            // multiply frequency of each element
            // and accumulate it in 'count'
            count *= entry.Value;
        }
          
        // required number of subsequences
        return count;
    }
  
    // Driver Code
    public static void Main(String[] args) 
    {
        int[] arr = { 4, 7, 6, 7 };
        int n = arr.Length;
        Console.WriteLine("Count = " + 
                           countSubseq(arr, n));
    }
}
  
// This code is contributed by Princi Singh


输出:

Count = 2

时间复杂度: O(n)。
辅助空间: O(n)。