Dynamic programmingAnimated
Longest Increasing Subsequence (LIS)
Longest subsequence in strictly increasing order.
Time
O(n²), O(n log n) with patience
Extra space
O(n)
Use it for
Ordering and scheduling problems.
How it works
- Find the longest subsequence whose values strictly increase.
- Let L(i) be the length of the longest increasing subsequence ending at index i. Start with 1.
- For every earlier index j with a[j] < a[i], L(i) = max(L(i), L(j) + 1).
- The answer is the biggest L(i). O(n²) this way (O(n log n) is possible with binary search).
Watch it run
Loading animation…