给定一个大小为N的数组arr [] ,任务是找到在最后N次将arr []与其自身串联而成的数组中最长的递增子序列的长度。
例子:
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的数组中找到最长的增长子集是很有效的,这可以通过找到数组的所有唯一元素来找到。
以下是有效方法的算法:
算法:
- 将数组的唯一元素存储在以(元素,计数)作为(键,值)对的映射中。
- 对于数组中的每个元素
- 如果地图中不存在当前元素,则将其插入地图中,计数为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
性能分析:
- 时间复杂度:与上述方法一样,在最坏的情况下,只有一个循环占用O(N)时间,因此时间复杂度将为O(N) 。
- 空间复杂度:与上述方法一样,在最坏的情况下使用了一个哈希表可以占用O(N)空间,因此空间复杂度将为O(N)