📌  相关文章
📜  通过将数组与其自身串联N次而形成的最长递增子序列

📅  最后修改于: 2021-04-24 16:43:44             🧑  作者: Mango

给定一个大小为N的数组arr [] ,任务是找到在最后N次将arr []与其自身串联而成的数组中最长的递增子序列的长度。
例子:

天真的方法:
解决此问题的基本方法是通过将给定数组连接到自身N次来创建最终数组,然后在其中找到最长的递增子序列。
时间复杂度: O(N 2 )
辅助空间: O(N 2 )

高效方法:
根据有效的方法,存在于最长增加的子序列中的任何元素只能存在一次。这意味着元素重复N次不会影响子序列,但是任何元素都可以随时选择。因此,在长度为N的数组中找到最长的增长子集是很有效的,这可以通过找到数组的所有唯一元素来找到。
以下是有效方法的算法:
算法:

  • 将数组的唯一元素存储在以(元素,计数)作为(键,值)对的映射中。
  • 对于数组中的每个元素
    • 如果地图中不存在当前元素,则将其插入地图中,计数为1。
    • 否则,增加数组中数组元素的数量。
  • 找到地图的长度,这将是所需的答案。

例如:

下面是上述方法的实现:

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)