原文:
给定 n 个元素,编写一个程序,打印相邻元素差为 1 的最长递增子序列。 例:
输入:a[] = {3,10,3,11,4,5,6,7,8,12} 输出:3 4 5 6 7 8 说明:3,4,5,6,7,8 是相邻元素相差 1 的最长递增子序列。 输入:a[] = {6,7,8,3,4,5,9,10} 输出:6 7 8 9 10 说明:6,7,8,9,10 是最长的递增子序列
我们已经讨论了的长度。为了打印子序列,我们存储最后一个元素的索引。然后我们打印以最后一个元素结尾的连续元素。 下面给出的是上述方法的实施:
c
// cpp program to find length of the
// longest increasing subsequence
// whose adjacent element differ by 1
#include
using namespace std;
// function that returns the length of the
// longest increasing subsequence
// whose adjacent element differ by 1
void longestsubsequence(int a[], int n)
{
// stores the index of elements
unordered_map mp;
// stores the length of the longest
// subsequence that ends with a[i]
int dp[n];
memset(dp, 0, sizeof(dp));
int maximum = int_min;
// iterate for all element
int index = -1;
for (int i = 0; i < n; i ) {
// if a[i]-1 is present before i-th index
if (mp.find(a[i] - 1) != mp.end()) {
// last index of a[i]-1
int lastindex = mp[a[i] - 1] - 1;
// relation
dp[i] = 1 dp[lastindex];
}
else
dp[i] = 1;
// stores the index as 1-index as we need to
// check for occurrence, hence 0-th index
// will not be possible to check
mp[a[i]] = i 1;
// stores the longest length
if (maximum < dp[i]) {
maximum = dp[i];
index = i;
}
}
// we know last element of sequence is
// a[index]. we also know that length
// of subsequence is "maximum". so we
// print these many consecutive elements
// starting from "a[index] - maximum 1"
// to a[index].
for (int curr = a[index] - maximum 1;
curr <= a[index]; curr )
cout << curr << " ";
}
// driver code
int main()
{
int a[] = { 3, 10, 3, 11, 4, 5, 6, 7, 8, 12 };
int n = sizeof(a) / sizeof(a[0]);
longestsubsequence(a, n);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program to find length of the
// longest increasing subsequence
// whose adjacent element differ by
import java.util.hashmap;
class gfg
{
// function that returns the length of the
// longest increasing subsequence
// whose adjacent element differ by 1
public static void longestsubsequence(int[] a,
int n)
{
// stores the index of elements
hashmap mp = new hashmap<>();
// stores the length of the longest
// subsequence that ends with a[i]
int[] dp = new int[n];
int maximum = integer.min_value;
// iterate for all element
int index = -1;
for(int i = 0; i < n; i )
{
// if a[i]-1 is present before i-th index
if (mp.get(a[i] - 1) != null)
{
// last index of a[i]-1
int lastindex = mp.get(a[i] - 1) - 1;
// relation
dp[i] = 1 dp[lastindex];
}
else
dp[i] = 1;
// stores the index as 1-index as we need to
// check for occurrence, hence 0-th index
// will not be possible to check
mp.put(a[i], i 1);
// stores the longest length
if (maximum < dp[i])
{
maximum = dp[i];
index = i;
}
}
// we know last element of sequence is
// a[index]. we also know that length
// of subsequence is "maximum". so we
// print these many consecutive elements
// starting from "a[index] - maximum 1"
// to a[index].
for (int curr = a[index] - maximum 1;
curr <= a[index]; curr )
system.out.print(curr " ");
}
// driver code
public static void main(string[] args)
{
int[] a = { 3, 10, 3, 11, 4,
5, 6, 7, 8, 12 };
int n = a.length;
longestsubsequence(a, n);
}
}
// this code is contributed by sanjeev2552
python 3
# python 3 program to find length of
# the longest increasing subsequence
# whose adjacent element differ by 1
import sys
# function that returns the length
# of the longest increasing subsequence
# whose adjacent element differ by 1
def longestsubsequence(a, n):
# stores the index of elements
mp = {i:0 for i in range(13)}
# stores the length of the longest
# subsequence that ends with a[i]
dp = [0 for i in range(n)]
maximum = -sys.maxsize - 1
# iterate for all element
index = -1
for i in range(n):
# if a[i]-1 is present before
# i-th index
if ((a[i] - 1 ) in mp):
# last index of a[i]-1
lastindex = mp[a[i] - 1] - 1
# relation
dp[i] = 1 dp[lastindex]
else:
dp[i] = 1
# stores the index as 1-index as we
# need to check for occurrence, hence
# 0-th index will not be possible to check
mp[a[i]] = i 1
# stores the longest length
if (maximum < dp[i]):
maximum = dp[i]
index = i
# we know last element of sequence is
# a[index]. we also know that length
# of subsequence is "maximum". so we
# print these many consecutive elements
# starting from "a[index] - maximum 1"
# to a[index].
for curr in range(a[index] - maximum 1,
a[index] 1, 1):
print(curr, end = " ")
# driver code
if __name__ == '__main__':
a = [3, 10, 3, 11, 4, 5,
6, 7, 8, 12]
n = len(a)
longestsubsequence(a, n)
# this code is contributed by
# surendra_gangwar
c
// c# program to find length of the
// longest increasing subsequence
// whose adjacent element differ by
using system;
using system.collections.generic;
class gfg
{
// function that returns the length of the
// longest increasing subsequence
// whose adjacent element differ by 1
static void longestsubsequence(int[] a, int n)
{
// stores the index of elements
dictionary mp = new dictionary();
// stores the length of the longest
// subsequence that ends with a[i]
int[] dp = new int[n];
int maximum = -100000000;
// iterate for all element
int index = -1;
for(int i = 0; i < n; i )
{
// if a[i]-1 is present before i-th index
if (mp.containskey(a[i] - 1) == true)
{
// last index of a[i]-1
int lastindex = mp[a[i] - 1] - 1;
// relation
dp[i] = 1 dp[lastindex];
}
else
dp[i] = 1;
// stores the index as 1-index as we need to
// check for occurrence, hence 0-th index
// will not be possible to check
mp[a[i]] = i 1;
// stores the longest length
if (maximum < dp[i])
{
maximum = dp[i];
index = i;
}
}
// we know last element of sequence is
// a[index]. we also know that length
// of subsequence is "maximum". so we
// print these many consecutive elements
// starting from "a[index] - maximum 1"
// to a[index].
for (int curr = a[index] - maximum 1;
curr <= a[index]; curr )
console.write(curr " ");
}
// driver code
static void main()
{
int[] a = { 3, 10, 3, 11, 4,
5, 6, 7, 8, 12 };
int n = a.length;
longestsubsequence(a, n);
}
}
// this code is contributed by mohit kumar
java 描述语言
输出:
3 4 5 6 7 8
时间复杂度:o(nlogn) t3】辅助空间: o(n)
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处