给定一个大小为N的数组arr[] ,任务是打印数组中存在的严格递增子序列的最小可能计数。
注意:可以交换数组元素对。
例子:
Input: arr[] = {2, 1, 2, 1, 4, 3}
Output: 2
Explanation: Sorting the array modifies the array to arr[] = {1, 1, 2, 2, 3, 4}. Two possible increasing subsequences are {1, 2, 3} and {1, 2, 4}, which involves all the array elements.
Input: arr[] = {3, 3, 3}
Output: 3
MultiSet-based Approach:参考前一篇文章,解决使用Multiset寻找数组中最长递减子序列的问题。
时间复杂度: O(N 2 )
辅助空间: O(N)
空间优化方法:最佳想法基于以下观察:
Two elements with the same value can’t be included in a single subsequence, as they won’t form a strictly increasing subsequence.
Therefore, for every distinct array element, count its frequency, say y. Therefore, at least y subsequences are required.
Hence, the frequency of the most occurring array element is the required answer.
请按照以下步骤解决问题:
- 初始化一个变量,比如count ,以存储严格递增的子序列的最终计数。
- 遍历数组arr[] 并执行以下观察:
- 初始化两个变量,比如X来存储当前数组元素,以及freqX来存储当前数组元素的频率。
- 在freqX 中查找并存储当前元素的所有出现。
- 如果当前元素的频率大于前一个计数,则更新计数。
- 打印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