Longest Increasing Subsequence (LIS)
Longest Increasing Subsequence (LIS) finds the length of the longest subsequence in an array where all elements are in strictly increasing order. Unlike a subarray, a subsequence doesn't need to be contiguous - elements can be selected from anywhere as long as their order is preserved. This classic DP problem has two solutions: O(n²) dynamic programming and O(n log n) binary search with patience sorting. Applications include stock price analysis, scheduling, Box Stacking Problem, and Building Bridges Problem.
Visualization
Interactive visualization for Longest Increasing Subsequence (LIS)
Longest Increasing Subsequence (LIS)
Array:
• Time: O(n²) - DP approach
• Space: O(n)
• O(n log n) possible with binary search
Interactive visualization with step-by-step execution
Implementation
1// O(n²) DP solution
2function lisDP(arr: number[]): number {
3 const n = arr.length;
4 const dp: number[] = Array(n).fill(1);
5
6 for (let i = 1; i < n; i++) {
7 for (let j = 0; j < i; j++) {
8 if (arr[j] < arr[i]) {
9 dp[i] = Math.max(dp[i], dp[j] + 1);
10 }
11 }
12 }
13
14 return Math.max(...dp);
15}
16
17// O(n log n) Binary Search solution
18function lisBinarySearch(arr: number[]): number {
19 const tails: number[] = [];
20
21 for (const num of arr) {
22 let left = 0;
23 let right = tails.length;
24
25 while (left < right) {
26 const mid = Math.floor((left + right) / 2);
27 if (tails[mid] < num) {
28 left = mid + 1;
29 } else {
30 right = mid;
31 }
32 }
33
34 if (left === tails.length) {
35 tails.push(num);
36 } else {
37 tails[left] = num;
38 }
39 }
40
41 return tails.length;
42}Deep Dive
Theoretical Foundation
LIS demonstrates optimal substructure: if we know LIS ending at position i, we can extend it by appending arr[j] where j > i and arr[j] > arr[i]. **O(n²) DP**: dp[i] = length of LIS ending at i. For each i, check all j < i: if arr[j] < arr[i], dp[i] = max(dp[i], dp[j]+1). Answer is max(dp). **O(n log n)**: Maintain array 'tails' where tails[i] = smallest tail element of all increasing subsequences of length i+1. For each number, binary search its position in tails (first element >= number) and replace. This greedy approach works because smaller tails give more room for future extensions. Tails length = LIS length. This technique is called 'patience sorting' and is used in real solitaire games!
Complexity
Time
O(n log n)
O(n²) or O(n log n)
O(n²)
Space
O(n)
Applications
Industry Use
Stock price analysis (longest profit-making sequence)
Patience sorting card game strategy
Box stacking problem (3D version)
Building bridges without crossing
Scheduling tasks with dependencies
Version control (longest consistent chain)
Bioinformatics sequence analysis
Use Cases
Related Algorithms
0/1 Knapsack Problem
A classic optimization problem where you must select items with given weights and values to maximize total value without exceeding the knapsack's capacity. Each item can be taken only once (0 or 1). This is a fundamental problem in combinatorial optimization, resource allocation, and decision-making scenarios.
Longest Common Subsequence (LCS)
Finds the longest subsequence common to two sequences. A subsequence is a sequence that appears in the same relative order but not necessarily contiguously. This is fundamental in diff utilities, DNA sequence analysis, and version control systems like Git.
Edit Distance (Levenshtein Distance)
Edit Distance, also known as Levenshtein Distance, computes the minimum number of single-character edits (insertions, deletions, or substitutions) required to transform one string into another. Named after Soviet mathematician Vladimir Levenshtein who introduced it in 1965, this fundamental algorithm has applications in spell checking, DNA sequence analysis, natural language processing, and plagiarism detection.
Coin Change Problem
The Coin Change Problem finds the minimum number of coins needed to make a given amount using unlimited supplies of given coin denominations. It's a classic example of both dynamic programming and greedy algorithms, with two main variants: finding the minimum number of coins (optimization) and counting the number of ways to make change (counting). This problem has direct applications in currency systems, vending machines, and resource optimization.