给定一个由 n 个正整数组成的数组。我们需要找到最大的连续正整数递增序列。
例子:
Input : arr[] = {5, 7, 6, 7, 8}
Output : Size of LIS = 4
LIS = 5, 6, 7, 8
Input : arr[] = {5, 7, 8, 7, 5}
Output : Size of LIS = 2
LIS = 7, 8
这个问题可以通过 LIS 的概念轻松解决,其中每个下一个更大的元素与之前的元素相差 1。但这将需要 O(n^2) 时间复杂度。
通过使用散列,我们可以找到时间复杂度为 O(n) 的连续整数的最长递增序列的大小。
我们创建一个哈希表.. 现在对于每个元素 arr[i],我们执行 hash[arr[i]] = hash[arr[i] – 1] + 1。所以,对于我们知道的每个元素最长连续递增子序列结束用它。最后我们从哈希表中返回最大值。
C++
// C++ implementation of longest continuous increasing
// subsequence
#include
using namespace std;
// Function for LIS
int findLIS(int A[], int n)
{
unordered_map hash;
// Initialize result
int LIS_size = 1;
int LIS_index = 0;
hash[A[0]] = 1;
// iterate through array and find
// end index of LIS and its Size
for (int i = 1; i < n; i++) {
hash[A[i]] = hash[A[i] - 1] + 1;
if (LIS_size < hash[A[i]]) {
LIS_size = hash[A[i]];
LIS_index = A[i];
}
}
// print LIS size
cout << "LIS_size = " << LIS_size << "\n";
// print LIS after setting start element
cout << "LIS : ";
int start = LIS_index - LIS_size + 1;
while (start <= LIS_index) {
cout << start << " ";
start++;
}
}
// driver
int main()
{
int A[] = { 2, 5, 3, 7, 4, 8, 5, 13, 6 };
int n = sizeof(A) / sizeof(A[0]);
findLIS(A, n);
return 0;
}
Java
// Java implementation of longest continuous increasing
// subsequence
import java.util.*;
class GFG
{
// Function for LIS
static void findLIS(int A[], int n)
{
Map hash = new HashMap();
// Initialize result
int LIS_size = 1;
int LIS_index = 0;
hash.put(A[0], 1);
// iterate through array and find
// end index of LIS and its Size
for (int i = 1; i < n; i++)
{
hash.put(A[i], hash.get(A[i] - 1)==null? 1:hash.get(A[i] - 1)+1);
if (LIS_size < hash.get(A[i]))
{
LIS_size = hash.get(A[i]);
LIS_index = A[i];
}
}
// print LIS size
System.out.println("LIS_size = " + LIS_size);
// print LIS after setting start element
System.out.print("LIS : ");
int start = LIS_index - LIS_size + 1;
while (start <= LIS_index)
{
System.out.print(start + " ");
start++;
}
}
// Driver code
public static void main(String[] args)
{
int A[] = { 2, 5, 3, 7, 4, 8, 5, 13, 6 };
int n = A.length;
findLIS(A, n);
}
}
// This code is contributed by Princi Singh
Python3
# Python3 implementation of longest
# continuous increasing subsequence
# Function for LIS
def findLIS(A, n):
hash = dict()
# Initialize result
LIS_size, LIS_index = 1, 0
hash[A[0]] = 1
# iterate through array and find
# end index of LIS and its Size
for i in range(1, n):
# If the desired key is not present
# in dictionary, it will throw key error,
# to avoid this error this is necessary
if A[i] - 1 not in hash:
hash[A[i] - 1] = 0
hash[A[i]] = hash[A[i] - 1] + 1
if LIS_size < hash[A[i]]:
LIS_size = hash[A[i]]
LIS_index = A[i]
# print LIS size
print("LIS_size =", LIS_size)
# print LIS after setting start element
print("LIS : ", end = "")
start = LIS_index - LIS_size + 1
while start <= LIS_index:
print(start, end = " ")
start += 1
# Driver Code
if __name__ == "__main__":
A = [ 2, 5, 3, 7, 4, 8, 5, 13, 6 ]
n = len(A)
findLIS(A, n)
# This code is contributed by sanjeev2552
C#
// C# implementation of longest continuous increasing
// subsequence
using System;
using System.Collections.Generic;
class GFG
{
// Function for LIS
static void findLIS(int []A, int n)
{
Dictionary hash = new Dictionary();
// Initialize result
int LIS_size = 1;
int LIS_index = 0;
hash.Add(A[0], 1);
// iterate through array and find
// end index of LIS and its Size
for (int i = 1; i < n; i++)
{
if(hash.ContainsKey(A[i]-1))
{
var val = hash[A[i]-1];
hash.Remove(A[i]);
hash.Add(A[i], val + 1);
}
else
{
hash.Add(A[i], 1);
}
if (LIS_size < hash[A[i]])
{
LIS_size = hash[A[i]];
LIS_index = A[i];
}
}
// print LIS size
Console.WriteLine("LIS_size = " + LIS_size);
// print LIS after setting start element
Console.Write("LIS : ");
int start = LIS_index - LIS_size + 1;
while (start <= LIS_index)
{
Console.Write(start + " ");
start++;
}
}
// Driver code
public static void Main(String[] args)
{
int []A = { 2, 5, 3, 7, 4, 8, 5, 13, 6 };
int n = A.Length;
findLIS(A, n);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
LIS_size = 5
LIS : 2 3 4 5 6
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。