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

📅  最后修改于: 2021-09-04 08:15:49             🧑  作者: 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


C++
#include 
using namespace std;
 
int main() {
 
    cout<<"GFG!";
    return 0;
}


Javascript


输出:
5

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live