📌  相关文章
📜  最小化数组中严格增加的子序列的数量|套装2

📅  最后修改于: 2021-04-17 18:01:41             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是打印数组中存在的严格增加的子序列的最小可能计数。
注意:可以交换成对的数组元素。

例子:

基于MultiSet的方法:请参阅上一篇文章,以使用Multiset解决问题的方法,以找到数组中最长的递减子序列。
时间复杂度: O(N 2 )
辅助空间: O(N)

空间优化方法:最佳思想基于以下观察:

请按照以下步骤解决问题:

  1. 初始化一个变量,例如count ,以存储严格增加的子序列的最终计数。
  2. 遍历数组arr []并执行以下观察:
    • 初始化两个变量,例如X ,以存储当前数组元素,并初始化freqX ,以存储当前数组元素的频率。
    • freqX中查找并存储当前元素的所有出现。
    • 如果当前元素的频率大于上一个计数,则更新计数
  3. 打印count的值。

下面是上述方法的实现:

C++
// C++ program for
// the above approach
 
#include 
using namespace std;
 
// Function to find the number of strictly
// increasing subsequences in an array
int minimumIncreasingSubsequences(
    int arr[], int N)
{
    // Sort the array
    sort(arr, arr + N);
 
    // Stores final count
    // of subsequences
    int count = 0;
    int i = 0;
 
    // Traverse the array
    while (i < N) {
 
        // Stores current element
        int x = arr[i];
 
        // Stores frequency of
        // the current element
        int freqX = 0;
 
        // Count frequency of
        // the current element
        while (i < N && arr[i] == x) {
            freqX++;
            i++;
        }
 
        // If current element frequency
        // is greater than count
        count = max(count, freqX);
    }
 
    // Print the final count
    cout << count;
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 2, 1, 2, 1, 4, 3 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call to find
    // the number of strictly
    // increasing subsequences
    minimumIncreasingSubsequences(arr, N);
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
   
// Function to find the number of strictly
// increasing subsequences in an array
static void minimumIncreasingSubsequences(
    int arr[], int N)
{
   
    // Sort the array
    Arrays.sort(arr);
 
    // Stores final count
    // of subsequences
    int count = 0;
    int i = 0;
 
    // Traverse the array
    while (i < N)
    {
 
        // Stores current element
        int x = arr[i];
 
        // Stores frequency of
        // the current element
        int freqX = 0;
 
        // Count frequency of
        // the current element
        while (i < N && arr[i] == x)
        {
            freqX++;
            i++;
        }
 
        // If current element frequency
        // is greater than count
        count = Math.max(count, freqX);
    }
 
    // Print the final count
    System.out.print(count);
}
 
// Driver Code
public static void main(String args[])
{
    // Given array
    int arr[] = { 2, 1, 2, 1, 4, 3 };
 
    // Size of the array
    int N = arr.length;
 
    // Function call to find
    // the number of strictly
    // increasing subsequences
    minimumIncreasingSubsequences(arr, N);
}
}
 
// This code is contributed by splevel62.


Python3
# Python3 program to implement
# the above approach
 
# Function to find the number of strictly
# increasing subsequences in an array
def minimumIncreasingSubsequences(arr, N) :
 
    # Sort the array
    arr.sort()
  
    # Stores final count
    # of subsequences
    count = 0
    i = 0
  
    # Traverse the array
    while (i < N) :
  
        # Stores current element
        x = arr[i]
  
        # Stores frequency of
        # the current element
        freqX = 0
  
        # Count frequency of
        # the current element
        while (i < N and arr[i] == x) :
            freqX += 1
            i += 1
  
        # If current element frequency
        # is greater than count
        count = max(count, freqX)
  
    # Print the final count
    print(count)
 
# Given array
arr = [ 2, 1, 2, 1, 4, 3 ]
 
# Size of the array
N = len(arr)
 
# Function call to find
# the number of strictly
# increasing subsequences
minimumIncreasingSubsequences(arr, N)
 
# This code is contributed by divyesh072019.


C#
// C# program to implement
// the above approach
using System;
 
public class GFG
{
   
// Function to find the number of strictly
// increasing subsequences in an array
static void minimumIncreasingSubsequences(
    int []arr, int N)
{
   
    // Sort the array
    Array.Sort(arr);
 
    // Stores readonly count
    // of subsequences
    int count = 0;
    int i = 0;
 
    // Traverse the array
    while (i < N)
    {
 
        // Stores current element
        int x = arr[i];
 
        // Stores frequency of
        // the current element
        int freqX = 0;
 
        // Count frequency of
        // the current element
        while (i < N && arr[i] == x)
        {
            freqX++;
            i++;
        }
 
        // If current element frequency
        // is greater than count
        count = Math.Max(count, freqX);
    }
 
    // Print the readonly count
    Console.Write(count);
}
 
// Driver Code
public static void Main(String []args)
{
   
    // Given array
    int []arr = { 2, 1, 2, 1, 4, 3 };
 
    // Size of the array
    int N = arr.Length;
 
    // Function call to find
    // the number of strictly
    // increasing subsequences
    minimumIncreasingSubsequences(arr, N);
}
}
 
// This code is contributed by 29AjayKumar


输出:
2

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