给定一个包含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.
处理方法:上述问题是在置换子序列上,所以数组元素的顺序无关紧要,重要的是每个元素的频率。如果数组的长度为 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
Javascript
输出:
3
时间复杂度: O(N)
辅助空间复杂度: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live