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

📅  最后修改于: 2021-09-06 11:31:16             🧑  作者: Mango

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

例子:

MultiSet-based Approach:参考前一篇文章,解决使用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


Javascript


输出:
2

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

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