📜  最长递增连续子序列 |第 2 组

📅  最后修改于: 2022-05-13 01:56:07.423000             🧑  作者: Mango

最长递增连续子序列 |第 2 组

给定一个包含N个元素的数组arr[] ,任务是找到相邻元素差为 1 的最长递增子序列的长度。

例子:

方法:本文已经讨论了朴素和动态编程方法。本文讨论了一种使用散列的更简单且易于实现的方法。这个想法是以当前整数X结尾的子序列的长度存储在无序映射m中。因此,如果X + 1在遍历期间发生,则子序列的长度将为m[X] + 1 。地图m中的最大值是所需答案。

下面是上述方法的实现:

C++
// C++ program of the above approach
#include 
using namespace std;
 
// Function to find the longest
// increasing consecutive subsequence
int longestSubsequence(int arr[], int N)
{
    // Stores the length of longest
    // subsequence qnding with key
    unordered_map m;
 
    // Stores the required answer
    int ans = 0;
 
    // Loop to traverse array
    for (int i = 0; i < N; i++) {
        // Length of subsequence
        // ending with arr[i]
        m[arr[i]] = max(m[arr[i]],
                        m[arr[i] - 1] + 1);
 
        // Update Answer
        ans = max(ans, m[arr[i]]);
    }
 
    // Return Answer
    return ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 4, 2, 6, 5, 3, 7, 4, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << longestSubsequence(arr, N);
 
    return 0;
}


Java
// JAVA program of the above approach
import java.util.*;
class GFG
{
 
  // Function to find the longest
  // increasing consecutive subsequence
  public static int longestSubsequence(int[] arr, int N)
  {
 
    // Stores the length of longest
    // subsequence qnding with key
    HashMap m = new HashMap<>();
 
    // Stores the required answer
    int ans = 0;
 
    // Loop to traverse array
    for (int i = 0; i < N; i++) {
      // Length of subsequence
      // ending with arr[i]
      if (m.containsKey(arr[i])) {
        m.put(arr[i],
              Math.max(m.get(arr[i]),
                       m.get(arr[i] - 1) + 1));
      }
      else {
        m.put(arr[i], 1);
      }
      // Update Answer
      ans = Math.max(ans, m.get(arr[i]));
    }
 
    // Return Answer
    return ans;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int[] arr = new int[] { 3, 4, 2, 6, 5, 3, 7, 4, 5 };
    int N = arr.length;
 
    System.out.print(longestSubsequence(arr, N));
  }
}
 
// This code is contributed by Taranpreet


Python
# Python program of the above approach
 
# Function to find the longest
# increasing consecutive subsequence
def longestSubsequence(arr, N):
 
    # Stores the length of longest
    # subsequence qnding with key
    m = {}
 
    # Stores the required answer
    ans = 0
 
    # Loop to traverse array
    for i in range(0, N):
 
        # Length of subsequence
        # ending with arr[i]
        if arr[i] in m and arr[i] - 1 in m:
            m[arr[i]] = max(m[arr[i]], m[arr[i] - 1] + 1)
        else:
            m[arr[i]] = 1
 
        # Update Answer
        ans = max(ans, m[arr[i]])
 
    # Return Answer
    return ans
 
# Driver Code
arr = [3, 4, 2, 6, 5, 3, 7, 4, 5]
N = len(arr)
 
print(longestSubsequence(arr, N))
 
# This code is contributed by Samim Hossain Mondal.


C#
// C# program of the above approach
using System;
using System.Collections.Generic;
 
public class GFG
{
 
  // Function to find the longest
  // increasing consecutive subsequence
  public static int longestSubsequence(int[] arr, int N)
  {
 
    // Stores the length of longest
    // subsequence qnding with key
    Dictionary m = new Dictionary();
 
    // Stores the required answer
    int ans = 0;
 
    // Loop to traverse array
    for (int i = 0; i < N; i++) {
      // Length of subsequence
      // ending with arr[i]
      if (m.ContainsKey(arr[i])) {
        m[arr[i]]=
          Math.Max(m[arr[i]],
                   m[arr[i] - 1] + 1);
      }
      else {
        m.Add(arr[i], 1);
      }
      // Update Answer
      ans = Math.Max(ans, m[arr[i]]);
    }
 
    // Return Answer
    return ans;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int[] arr = new int[] { 3, 4, 2, 6, 5, 3, 7, 4, 5 };
    int N = arr.Length;
 
    Console.Write(longestSubsequence(arr, N));
  }
}
 
// This code is contributed by shikhasingrajput


Javascript


输出
4

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