Skip to content
StackDecode
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

  1. Find the longest subsequence whose values strictly increase.
  2. Let L(i) be the length of the longest increasing subsequence ending at index i. Start with 1.
  3. For every earlier index j with a[j] < a[i], L(i) = max(L(i), L(j) + 1).
  4. 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…