📌  相关文章
📜  由不同元素组成的最长子序列的长度

📅  最后修改于: 2021-05-17 06:20:11             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是找到仅由不同元素组成的最长子序列的长度。

例子:

天真的方法:最简单的方法是生成数组的所有子序列,并检查它是否仅由不同的元素组成。不断更新获得的此类子序列的最大长度。最后,打印获得的最大长度。

时间复杂度: O(2 N )
辅助空间: O(1)

高效方法:仅包含不同元素的最长子序列的长度将等于数组中不同元素的数量。请按照以下步骤解决问题:

  1. 遍历给定的数组,继续将遇到的元素插入到Hashset中。
  2. 由于HashSet仅包含唯一元素,因此在完成遍历数组后,将HashSet的大小打印为必需的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find length of
// the longest subsequence
// consisting of distinct elements
int longestSubseq(int arr[], int n)
{
    // Stores the distinct
    // array elements
    unordered_set s;
 
    // Traverse the input array
    for (int i = 0; i < n; i++) {
 
        // If current element has not
        // occurred previously
        if (s.find(arr[i]) == s.end()) {
 
            // Insert it into set
            s.insert(arr[i]);
        }
    }
 
    return s.size();
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 1, 2, 3, 3, 4, 5, 5, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << longestSubseq(arr, n);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to find length of
// the longest subsequence
// consisting of distinct elements
static int longestSubseq(int arr[], int n)
{
    // Stores the distinct
    // array elements
    Set s = new HashSet<>();
   
    // Traverse the input array
    for(int i = 0; i < n; i++)
    {
   
        // If current element has not
        // occurred previously
        if (!s.contains(arr[i]))
        {
   
            // Insert it into set
            s.add(arr[i]);
        }
    }
    return s.size();
}
 
// Driver code
public static void main (String[] args)
{
     
    // Given array
    int arr[] = { 1, 2, 3, 3, 4, 5, 5, 5 };
    int n = arr.length;
     
    // Function call
    System.out.println(longestSubseq(arr, n));    
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for
# the above approach
 
# Function to find length of
# the longest subsequence
# consisting of distinct elements
def longestSubseq(arr, n):
 
    # Stores the distinct
    # array elements
    s = set()
 
    # Traverse the input array
    for i in range(n):
       
        # If current element has not
        # occurred previously
        if (arr[i] not in s):
 
            # Insert it into set
            s.add(arr[i])
 
    return len(s)
 
# Given array
arr = [1, 2, 3, 3,
       4, 5, 5, 5]
n = len(arr)
 
# Function Call
print(longestSubseq(arr, n))
 
# This code is contributed by divyeshrabadiya07


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
      
// Function to find length of
// the longest subsequence
// consisting of distinct elements
static int longestSubseq(int []arr, int n)
{
     
    // Stores the distinct
    // array elements
    HashSet s = new HashSet();
    
    // Traverse the input array
    for(int i = 0; i < n; i++)
    {
         
        // If current element has not
        // occurred previously
        if (!s.Contains(arr[i]))
        {
             
            // Insert it into set
            s.Add(arr[i]);
        }
    }
    return s.Count;
}
  
// Driver code
public static void Main(string[] args)
{
     
    // Given array
    int []arr = { 1, 2, 3, 3, 4, 5, 5, 5 };
    int n = arr.Length;
      
    // Function call
    Console.Write(longestSubseq(arr, n));    
}
}
 
// This code is contributed by rutvik_56


输出:
5




时间复杂度: O(N)
辅助空间: O(N)