📌  相关文章
📜  从给定数组中最大化减少子序列数

📅  最后修改于: 2021-04-23 17:12:33             🧑  作者: Mango

给定的阵列ARR [],任务是重新排列阵列,以产生最大减小子序列和打印的 子序列的最大数量,以使每个数组元素可以是单个子序列的一部分,并且子序列的长度需要最大化。

例子:

方法:
为了解决该问题,不需要实际重新排列给定的数组。由于每个元素都可以是单个子序列的一部分,因此子序列的数量将等于最频繁元素的频率。
请按照以下步骤解决问题:

  • 遍历数组并将每个数组元素的频率存储在哈希图中
  • 现在,遍历HashMap以找到数组中元素的最大频率。
  • 将最大频率打印为可能的最大递减子序列数。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
  
// Function to count maximum subsequence
void Maximum_subsequence(int A[], int N)
{
    // Stores the frequency
    // of array elements
    unordered_map frequency;
  
    // Stores max frequency
    int max_freq = 0;
  
    for (int i = 0; i < N; i++) {
  
        // Update frequency of A[i]
        frequency[A[i]]++;
    }
  
    for (auto it : frequency) {
  
        // Update max subsequence
        if (it.second > max_freq) {
            max_freq = it.second;
        }
    }
  
    // Print the count
    cout << max_freq << endl;
}
  
// Driver Code
int main()
{
    int arr[] = { 5, 2, 6, 5, 2, 4, 5, 2 };
  
    int N = sizeof(arr) / sizeof(arr[0]);
  
    Maximum_subsequence(arr, N);
  
    return 0;
}


Java
// Java program for rearrange array
// to generate maximum decreasing
// subsequences
import java.util.HashMap;
import java.util.Map;
public class Main {
    // Function to count maximum subsequence
    public static void Maximum_subsequence(
        int[] A, int N)
    {
        // Stores the frequency
        // of array elements
        HashMap frequency
            = new HashMap<>();
  
        // Stores maximum frequency
        int max_freq = 0;
  
        for (int i = 0; i < N; i++) {
  
            // Update frequency of A[i]
            if (frequency.containsKey(A[i])) {
                frequency.replace(A[i],
                                  frequency.get(A[i]) + 1);
            }
            else {
                frequency.put(A[i], 1);
            }
        }
  
        for (Map.Entry it : frequency.entrySet()) {
  
            // Update maximum subsequences
            if ((int)it.getValue() > max_freq) {
                max_freq = (int)it.getValue();
            }
        }
  
        // Print the result
        System.out.println(max_freq);
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 5, 2, 6, 5, 2, 4, 5, 2 };
  
        int N = arr.length;
  
        Maximum_subsequence(arr, N);
    }
}


Python3
# Python3 program to implement 
# the above approach 
  
# Function to count maximum subsequence 
def Maximum_subsequence(A, N):
  
    # Stores the frequency 
    # of array elements 
    frequency = dict(); 
  
    # Stores max frequency 
    max_freq = 0; 
  
    for i in range(N): 
        if (A[i] in frequency):
            frequency[A[i]] += 1
        else:
            frequency[A[i]] = 1
      
    for it in frequency:
  
        # Update max subsequence 
        if (frequency[it] > max_freq): 
            max_freq = frequency[it]; 
          
    # Print the count 
    print(max_freq); 
  
# Driver Code 
arr = [ 5, 2, 6, 5, 2, 4, 5, 2 ]; 
  
Maximum_subsequence(arr, len(arr)); 
  
# This code is contributed by grand_master


C#
// C# program for rearrange array
// to generate maximum decreasing
// subsequences
using System;
using System.Collections.Generic;
  
class GFG{
      
// Function to count maximum subsequence
public static void Maximum_subsequence(int[] A,
                                       int N)
{
      
    // Stores the frequency
    // of array elements
    Dictionary frequency = new Dictionary();
  
    // Stores maximum frequency
    int max_freq = 0;
  
    for(int i = 0; i < N; i++)
    {
          
        // Update frequency of A[i]
        if (frequency.ContainsKey(A[i])) 
        {
            frequency[A[i]] = frequency[A[i]] + 1;
        }
        else 
        {
            frequency.Add(A[i], 1);
        }
    }
  
    foreach(KeyValuePair it in frequency) 
    {
          
        // Update maximum subsequences
        if ((int)it.Value > max_freq)
        {
            max_freq = (int)it.Value;
        }
    }
  
    // Print the result
    Console.WriteLine(max_freq);
}
  
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 5, 2, 6, 5, 2, 4, 5, 2 };
  
    int N = arr.Length;
  
    Maximum_subsequence(arr, N);
}
}
  
// This code is contributed by Rajput-Ji


输出:
3

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