给定一个由n 个整数组成的数组。问题是找到子序列中相邻元素之间的差异为 0 或 1 的子序列的最大长度。需要 O(n) 的时间复杂度。
例子:
Input : arr[] = {2, 5, 6, 3, 7, 6, 5, 8}
Output : 5
The subsequence is {5, 6, 7, 6, 5}.
Input : arr[] = {-2, -1, 5, -1, 4, 0, 3}
Output : 4
The subsequence is {-2, -1, -1, 0}.
方法 1:以前在这篇文章中讨论过一种时间复杂度为 O(n 2 ) 的方法。
方法 2(高效方法):想法是创建一个具有(ele, len)形式的元组的哈希映射,其中len表示以元素ele结尾的最长子序列的长度。现在,对于每个元素 arr[i],我们可以在哈希表中找到值 arr[i]-1、arr[i] 和 arr[i]+1 的长度,并考虑其中的最大值。将此最大值设为max 。现在,以 arr[i] 结尾的最长子序列的长度将是max+1 。更新此长度以及哈希表中的元素 arr[i]。最后,哈希表中长度最大的元素给出最大长度的子序列。
C++
// C++ implementation to find maximum length
// subsequence with difference between adjacent
// elements as either 0 or 1
#include
using namespace std;
// function to find maximum length subsequence
// with difference between adjacent elements as
// either 0 or 1
int maxLenSub(int arr[], int n)
{
// hash table to map the array element with the
// length of the longest subsequence of which
// it is a part of and is the last element of
// that subsequence
unordered_map um;
// to store the maximum length subsequence
int maxLen = 0;
// traverse the array elements
for (int i=0; iJava
// Java implementation to find maximum length
// subsequence with difference between adjacent
// elements as either 0 or 1
import java.util.HashMap;
class GFG
{
// function to find maximum length subsequence
// with difference between adjacent elements as
// either 0 or 1
public static int maxLengthSub(int[] arr)
{
// to store the maximum length subsequence
int max_val = 0;
int start = 0;
// hash table to map the array element with the
// length of the longest subsequence of which
// it is a part of and is the last element of
// that subsequence
HashMap map = new HashMap<>();
// traverse the array elements
for (int i = 0; i < arr.length; i++)
{
// initialize current length
// for element arr[i] as 0
int temp = 0;
if (map.containsKey(arr[i] - 1))
{
temp = map.get(arr[i] - 1);
}
if (map.containsKey(arr[i]))
{
temp = Math.max(temp, map.get(arr[i]));
}
if (map.containsKey(arr[i] + 1))
{
temp = Math.max(temp, map.get(arr[i] + 1));
}
temp++;
// update maximum length
if (temp > max_val)
{
max_val = temp;
}
map.put(arr[i], temp);
}
// required maximum length subsequence
return max_val;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = {2, 5, 6, 3, 7, 6, 5, 8};
System.out.println(maxLengthSub(arr));
}
}
// This code is contributed
// by tushar jajodia
Python3
# Python3 implementation to find maximum
# length subsequence with difference between
# adjacent elements as either 0 or 1
from collections import defaultdict
# Function to find maximum length subsequence with
# difference between adjacent elements as either 0 or 1
def maxLenSub(arr, n):
# hash table to map the array element with the
# length of the longest subsequence of which it is a
# part of and is the last element of that subsequence
um = defaultdict(lambda:0)
# to store the maximum length subsequence
maxLen = 0
# traverse the array elements
for i in range(0, n):
# initialize current length
# for element arr[i] as 0
length = 0
# if 'arr[i]-1' is in 'um' and its length of
# subsequence is greater than 'len'
if (arr[i]-1) in um and length < um[arr[i]-1]:
length = um[arr[i]-1]
# if 'arr[i]' is in 'um' and its length of
# subsequence is greater than 'len'
if arr[i] in um and length < um[arr[i]]:
length = um[arr[i]]
# if 'arr[i]+1' is in 'um' and its length of
# subsequence is greater than 'len'
if (arr[i]+1) in um and length < um[arr[i]+1]:
length = um[arr[i]+1]
# update arr[i] subsequence length in 'um'
um[arr[i]] = length + 1
# update maximum length
if maxLen < um[arr[i]]:
maxLen = um[arr[i]]
# required maximum length subsequence
return maxLen
# Driver program to test above
if __name__ == "__main__":
arr = [2, 5, 6, 3, 7, 6, 5, 8]
n = len(arr)
print("Maximum length subsequence =", maxLenSub(arr, n))
# This code is contributed by Rituraj Jain
Javascript
输出:
Maximum length subsequence = 5
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。