给定一个大小为N的数组arr[] ,任务是找到数组中最长递增子序列的长度,该数组由 arr[] 在末尾 N 次与自身串联而成。
例子:
Input: arr[] = {3, 2, 1}, N = 3
Output: 3
Explanation:
The array formed by the concatenation –
{3, 2, 1, 3, 2, 1, 3, 2, 1}
The longest increasing subsequence that can be formed from this array is of length 3 which is {1, 2, 3}
Input: N = 3 arr[] = {3, 1, 4}
Output:
Explanation:
The array formed by concatenation –
{3, 1, 4, 3, 1, 4, 3, 1, 4}
The longest increasing subsequence that can be formed from this array is of length 3 which is {1, 3, 4}
天真的方法:
解决这个问题的基本方法是通过将给定数组连接到自身 N 次来创建最终数组,然后在其中找到最长的递增子序列。
时间复杂度: O(N 2 )
辅助空间: O(N 2 )
有效的方法:
根据有效方法,出现在最长递增子序列中的任何元素只能出现一次。这意味着元素重复 N 次不会影响子序列,但是,可以随时选择任何元素。因此,在长度为 N 的数组中找到最长递增子集将是有效的,这可以通过查找数组的所有唯一元素来找到。
以下是有效方法的算法:
算法:
- 将数组的唯一元素存储在映射中,将 (element, count) 作为 (key, value) 对。
- 对于数组中的每个元素
- 如果当前元素不存在于地图中,则将其插入到地图中,计数为 1。
- 否则,增加数组中数组元素的计数。
- 找到地图的长度,这将是所需的答案。
例如:
Given Array be – {4, 4, 1}
Creating the map of unique elements: {(4, 2), (1, 1)}
Length of the Map = 2
Hence the required longest subsequence = 2
下面是上述方法的实现:
C++
// C++ implementation to find the
// longest increasing subsequence
// in repeating element of array
#include
using namespace std;
// Function to find the LCS
int findLCS(int arr[], int n){
unordered_map mp;
// Loop to create frequency array
for (int i = 0; i < n; i++) {
mp[arr[i]]++;
}
return mp.size();
}
// Driver code
int main()
{
int n = 3;
int arr[] = {3, 2, 1};
cout<
Java
// Java implementation to find the
// longest increasing subsequence
// in repeating element of array
import java.util.*;
class GFG{
// Function to find the LCS
static int findLCS(int arr[], int n)
{
HashMap mp = new HashMap();
// Loop to create frequency array
for(int i = 0; i < n; i++)
{
if(mp.containsKey(arr[i]))
{
mp.put(arr[i], mp.get(arr[i]) + 1);
}
else
{
mp.put(arr[i], 1);
}
}
return mp.size();
}
// Driver code
public static void main(String[] args)
{
int n = 3;
int arr[] = { 3, 2, 1 };
System.out.print(findLCS(arr, n));
}
}
// This code is contributed by amal kumar choubey
Python3
# Python3 implementation to find the
# longest increasing subsequence
# in repeating element of array
# Function to find the LCS
def findLCS(arr, n):
mp = {}
# Loop to create frequency array
for i in range(n):
if arr[i] in mp:
mp[arr[i]] += 1
else:
mp[arr[i]] = 1
return len(mp)
# Driver code
n = 3
arr = [ 3, 2, 1 ]
print(findLCS(arr, n))
# This code is contributed by ng24_7
C#
// C# implementation to find the
// longest increasing subsequence
// in repeating element of array
using System;
using System.Collections.Generic;
class GFG{
// Function to find the LCS
static int findLCS(int []arr, int n)
{
Dictionary mp = new Dictionary();
// Loop to create frequency array
for(int i = 0; i < n; i++)
{
if(mp.ContainsKey(arr[i]))
{
mp[arr[i]] = mp[arr[i]] + 1;
}
else
{
mp.Add(arr[i], 1);
}
}
return mp.Count;
}
// Driver code
public static void Main(String[] args)
{
int n = 3;
int []arr = { 3, 2, 1 };
Console.Write(findLCS(arr, n));
}
}
// This code is contributed by amal kumar choubey
Javascript
性能分析:
- 时间复杂度:与上述方法一样,只有一个循环在最坏的情况下需要 O(N) 时间,因此时间复杂度将为O(N) 。
- 空间复杂度:与上述方法一样,使用了一个哈希映射,在最坏的情况下可以占用 O(N) 空间,因此空间复杂度将为O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live