最长单调递增子序列大小(N log N):简单实现
给定一个随机数数组,找出数组中最长的单调递增子序列 (LIS)。
如果您想了解 O(NlogN) 方法,这里解释得非常清楚。
在这篇文章中,讨论了使用 stl 的 O(NlogN) 方法的简单且省时的实现。以下是 LIS O(NlogN) 的代码:
C++
// C++ implementation
// to find LIS
#include
#include
#include
using namespace std;
// Return length of LIS in arr[] of size N
int lis(int arr[], int N)
{
int i;
set s;
set::iterator k;
for (i=0; i
Java
// Java implementation
// to find LIS
import java.util.*;
class GFG{
// Return length of LIS
// in arr[] of size N
static int lis(int arr[],
int N)
{
int i;
HashSet s = new HashSet<>();
for (i = 0; i < N; i++)
{
// Check if the element
// was actually inserted
// An element in set is
// not inserted if it is
// already present. Please see
// https://www.geeksforgeeks.
// org/set-insert-function-in-c-stl/
int k = 0;
int size = s.size();
if (s.add(arr[i]))
{
// Find the position of
// inserted element in iterator k
if(s.contains(arr[i]))
k++;
// Find the next
// greater element in set
// If the new element is not
// inserted at the end, then
// remove the greater element
// next to it.
if (size == s.size())
s.remove(k + 1);
}
}
// Note that set s may not contain
// actual LIS, but its size gives
// us the length of LIS
return s.size();
}
public static void main(String[] args)
{
int arr[] = {8, 9, 12, 10, 11};
int n = arr.length;
System.out.print(lis(arr, n) + "\n");
}
}
// This code is contributed by gauravrajput1
Python3
# Python implementation
# to find LIS
# Return length of LIS
# in arr of size N
def lis(arr, N):
s = set();
for i in range(N):
# Check if the element
# was actually inserted
# An element in set is
# not inserted if it is
# already present. Please see
# https:#www.geeksforgeeks.
# org/set-insert-function-in-c-stl/
k = 0;
size = len(s);
if (s.add(arr[i])):
# Find the position of
# inserted element in iterator k
if arr[i] in s:
k += 1;
# Find the next
# greater element in set
# If the new element is not
# inserted at the end, then
# remove the greater element
# next to it.
if (size == len(s)):
s.remove(k + 1);
# Note that set s may not contain
# actual LIS, but its size gives
# us the length of LIS
return len(s);
# Driver code
if __name__ == '__main__':
arr = [ 8, 9, 12, 10, 11 ];
n = len(arr);
print(lis(arr, n) ,"");
# This code is contributed by Rajput-Ji
C#
// C# implementation
// to find LIS
using System;
using System.Collections.Generic;
class GFG{
// Return length of LIS
// in arr[] of size N
static int lis(int[] arr, int N)
{
int i;
HashSet s = new HashSet();
for(i = 0; i < N; i++)
{
// Check if the element was actually inserted
// An element in set is not inserted if it is
// already present. Please see
// https://www.geeksforgeeks.org/set-insert-function-in-c-stl/
int k = 0;
int size = s.Count;
if (s.Add(arr[i]))
{
// Find the position of inserted
// element in iterator k
if (s.Contains(arr[i]))
k++;
// Find the next greater element in set
// If the new element is not inserted at
// the end, then remove the greater element
// next to it.
if (size == s.Count)
s.Remove(k + 1);
}
}
// Note that set s may not contain
// actual LIS, but its size gives
// us the length of LIS
return s.Count;
}
// Driver code
static public void Main()
{
int[] arr = { 8, 9, 12, 10, 11 };
int n = arr.Length;
Console.Write(lis(arr, n) + "\n");
}
}
// This code is contributed by avanitrachhadiya2155
Javascript
输出
4