给定包含N个元素的数组arr ,请找到最长子序列的长度,以使其为特定长度的有效排列。如果不存在这样的排列序列,则打印0。
例子:
Input: arr[] = {3, 2, 1, 6, 5}
Output: 3
Explanation:
Longest permutation subsequence will be [3, 2, 1].
Input: arr[]= {2, 3, 4, 5}
Output: 0
Explanation:
No valid permutation subsequence present as element 1 is missing.
方法:上述问题是在置换子序列上的,因此数组元素的顺序无关紧要,只有重要的是每个元素的频率。如果array的长度为N,则排列序列的最大可能长度为N ,最小可能长度为0 。如果长度L的子序列是有效置换,则应存在从1到L的所有元素。
- 计算数组中[1,N]范围内元素的频率
- 遍历数组中从1到N的所有元素,并对迭代进行计数,直到观察到0频率为止。如果元素的频率为“ 0”,则返回当前的迭代次数作为所需的长度。
下面是上述方法的实现:
C++
// C++ Program to find length of
// Longest Permutaion Subsequence
// in a given array
#include
using namespace std;
// Function to find the
// longest permutation subsequence
int longestPermutation(int a[], int n)
{
// Map data structure to
// count the frequency of each element
map freq;
for (int i = 0; i < n; i++) {
freq[a[i]]++;
}
int len = 0;
for (int i = 1; i <= n; i++) {
// If frequency of element is 0,
// then we can not move forward
// as every element should be present
if (freq[i] == 0) {
break;
}
// Increasing the length by one
len++;
}
return len;
}
// Driver Code
int main()
{
int arr[] = { 3, 2, 1, 6, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << longestPermutation(arr, n)
<< "\n";
return 0;
}
Java
// Java Program to find length of
// Longest Permutaion Subsequence
// in a given array
import java.util.*;
class GFG{
// Function to find the
// longest permutation subsequence
static int longestPermutation(int arr[], int n)
{
// Map data structure to
// count the frequency of each element
HashMap freq = new HashMap();
for (int i = 0; i < n; i++) {
if(freq.containsKey(arr[i])){
freq.put(arr[i], freq.get(arr[i])+1);
}else{
freq.put(arr[i], 1);
}
}
int len = 0;
for (int i = 1; i <= n; i++) {
// If frequency of element is 0,
// then we can not move forward
// as every element should be present
if (!freq.containsKey(i)) {
break;
}
// Increasing the length by one
len++;
}
return len;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, 2, 1, 6, 5 };
int n = arr.length;
System.out.print(longestPermutation(arr, n));
}
}
// This code is contributed by Rajput-Ji
C#
// C# Program to find length of
// longest Permutaion Subsequence
// in a given array
using System;
using System.Collections.Generic;
public class GFG{
// Function to find the
// longest permutation subsequence
static int longestPermutation(int []arr, int n)
{
// Map data structure to
// count the frequency of each element
Dictionary freq = new Dictionary();
for (int i = 0; i < n; i++) {
if(freq.ContainsKey(arr[i])){
freq[arr[i]] = freq[arr[i]] + 1;
}else{
freq.Add(arr[i], 1);
}
}
int len = 0;
for (int i = 1; i <= n; i++) {
// If frequency of element is 0,
// then we can not move forward
// as every element should be present
if (!freq.ContainsKey(i)) {
break;
}
// Increasing the length by one
len++;
}
return len;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 3, 2, 1, 6, 5 };
int n = arr.Length;
Console.Write(longestPermutation(arr, n));
}
}
// This code is contributed by 29AjayKumar
Python3
# Program to find length of
# Longest Permutaion Subsequence
# in a given array
from collections import defaultdict
# Function to find the
# longest permutation subsequence
def longestPermutation(a, n):
# Map data structure to
# count the frequency of each element
freq = defaultdict(int)
for i in range( n ):
freq[a[i]] += 1
length = 0
for i in range(1 , n + 1):
# If frequency of element is 0,
# then we can not move forward
# as every element should be present
if (freq[i] == 0):
break
# Increasing the length by one
length += 1
return length
# Driver Code
if __name__ == "__main__":
arr = [ 3, 2, 1, 6, 5 ]
n = len(arr)
print(longestPermutation(arr, n))
# This code is contributed by chitranayal
输出:
3
时间复杂度: O(N)
辅助空间复杂度: O(N)