Longest Increasing Subsequence (LIS)
Finds the length of the longest increasing subsequence you can pick while preserving order. dp[i] is defined as the subsolution 'length of the LIS ending at element i', and reusing the subsolutions of earlier, smaller elements pulls the O(2ⁿ) brute force down to O(n²).
01Longest Increasing Subsequence (LIS)
Explore How It WorksFinding the longest increasing subsequence. Fill in, from left to right, the length of the longest increasing run ending at each value.
Every cell starts at 1. Any value on its own already forms an increasing run of length 1.
Among the smaller values before 22, the longest run ends at 10. Extending it makes the length 2.
Nothing smaller comes before 9. With nothing to extend, the length stays at 1.
Among the smaller values before 33, the longest run ends at 22. Extending it makes the length 3.
Among the smaller values before 21, the longest run ends at 10. Extending it makes the length 2.
Among the smaller values before 50, the longest run ends at 33. Extending it makes the length 4.
Among the smaller values before 41, the longest run ends at 33. Extending it makes the length 4.
Among the smaller values before 60, the longest run ends at 50. Extending it makes the length 5.
The longest increasing subsequence is 10 → 22 → 33 → 50 → 60 — length 5. The largest value in the table is the answer.
02 Understand It Simply
For EveryoneFor each position it computes the length of the longest increasing subsequence ending there, building on the results already computed to its left.
Fills a table with the length of the longest increasing subsequence ending at each position.
Reusing earlier results makes it efficient.
- –Trend analysis
- –version-compatibility checks
- –sequence problems
03 Python Implementation
A clean, readable reference implementation of the core logic of Longest Increasing Subsequence (LIS).
04 Frequently Asked Questions
FAQWhat is Longest Increasing Subsequence (LIS)?+
Finds the length of the longest increasing subsequence you can pick while preserving order. dp[i] is defined as the subsolution 'length of the LIS ending at element i', and reusing the subsolutions of earlier, smaller elements pulls the O(2ⁿ) brute force down to O(n²).
What is the time complexity of Longest Increasing Subsequence (LIS)?+
The time complexity of Longest Increasing Subsequence (LIS) is O(n²). Follow the step-by-step visualization to see exactly why.
Where is Longest Increasing Subsequence (LIS) used?+
Trend analysis, version-compatibility checks, sequence problems.
What's a simple analogy for Longest Increasing Subsequence (LIS)?+
For each position it computes the length of the longest increasing subsequence ending there, building on the results already computed to its left.
