300. Longest increasing subsequence
Last updated
Was this helpful?
Last updated
Was this helpful?
Given an unsorted array of integers, find the length of longest increasing subsequence.
For example,
Given[10, 9, 2, 5, 3, 7, 101, 18]
,
The longest increasing subsequence is[2, 3, 7, 101]
, therefore the length is4
. Note that there may be more than one LIS combination, it is only necessary for you to return the length.
Your algorithm should run in O(n^2) complexity.
Follow up:Could you improve it to O(nlogn) time complexity?
Credits: Special thanks tofor adding this problem and creating all test cases.
Thoughts
O(n^2) solution with
(Original explanation) O(n^2)
: O(nlogn) (Original explanation)
Code Binary Search (Java) inspired by : “end element of smaller list is smaller than end elements of larger lists“.
using Arrays.binarySearch by :
Code Binary Search with O(nlogn) by , inspired by