📌  相关文章
📜  最大化给定数组中递减子序列的计数

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

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

例子:

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

  • 遍历数组,将每个数组元素出现的频率存入一个Hashmap
  • 现在,遍历 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


Javascript


输出:
3

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程