DSADPO(n²)

Longest Increasing Subsequence Visualizer

What is LIS

A subsequence keeps order but may skip values. dp[i] is the longest increasing subsequence that ends at i. Look left for a smaller nums[j] and extend dp[j].

  • dp[i] = 1 + max(dp[j] for j < i if nums[j] < nums[i]), or 1 if none.
  • Answer is the max of the whole dp array — the best ending is unknown in advance.
  • O(n log n) patience tails exist; this visualizer shows the O(n²) definition.

Brute force approach

From index i try every later j with nums[j] > nums[i] and recurse. Overlapping tails make it exponential.

At each index the brute-force choice is take the value, when it extends the previous pick, or skip it. The state (index, previous index) is solved more than once. Memoization stores that pair.

Try a small input

At most 4 values. 10 9 2 5 has length 2.

Answer: 0 Brute calls: 0 Memo calls: 0 Calls saved: 0
Recursive brute force
def lis(i, prev):
if i == n: return 0
return max(skip, take if nums[prev] < nums[i])
Step 0

Memoized approach

dp[i] = 1 + max(dp[j]) for j < i and nums[j] < nums[i]. Time O(n^2).

Each state is solved once. A dashed node is a cache hit: the call returns immediately and that branch stops.

Memoized recursion
def lis(i, prev, memo):
if (i, prev) in memo: return memo
if i == n: return 0
memo = max(skip, take)
return memo
Step 0

How this visualizer works

Array 10, 9, 2, 5, 3, 7. Endings settle at 1,1,1,2,2,3 — LIS length 3 (2,5,7 or 2,3,7). Step through the DP table. The live code on the right is the DP recurrence — brute force is shown in Implementations below so you can see why it TLE.

Interactive DP trace

dp_table.state

Input
DP table
Cell being filled
Source / window
Already computed

current_action

Load an input, then press Play or Step.

live_trace.py — DP fill

1def lis(nums):
2dp = [1]*len(nums)
3for i in range(len(nums)):
4for j in range(i):
5if nums[j] < nums[i]:
6dp[i] = max(dp[i], dp[j]+1)
7return max(dp) if dp else 0

stdout — decision log

// each table write is recorded here
Speed Ready

Pseudocode & Complexity

dp_recurrence.txt

1def lis(nums):
2dp = [1]*len(nums)
3for i in range(len(nums)):
4for j in range(i):
5if nums[j] < nums[i]:
6dp[i] = max(dp[i], dp[j]+1)
7return max(dp) if dp else 0

complexity.md

OperationTime
Pair (j, i)O(1)
All pairsO(n²)
SpaceO(n)

Brute force explores the same subproblems many times and will TLE on the hidden tests. The DP codes below reuse each answer once.

Implementations

dp.py

1def lis_brute(nums, i=0, prev=None):
2if i == len(nums):
3return 0
4skip = lis_brute(nums, i + 1, prev)
5take = 0
6if prev is None or nums[i] > prev:
7take = 1 + lis_brute(nums, i + 1, nums[i])
8return max(skip, take)
1def lis_dp(nums):
2if not nums:
3return 0
4n = len(nums)
5dp = [1] * n
6best = 1
7for i in range(n):
8for j in range(i):
9if nums[j] < nums[i] and dp[j] + 1 > dp[i]:
10dp[i] = dp[j] + 1
11if dp[i] > best:
12best = dp[i]
13return best
1int lis_brute(int *nums, int n, int i, int prev, int has_prev) {
2int skip;
3int take = 0;
4if (i == n) {
5return 0;
6}
7skip = lis_brute(nums, n, i + 1, prev, has_prev);
8if (!has_prev || nums[i] > prev) {
9take = 1 + lis_brute(nums, n, i + 1, nums[i], 1);
10}
11return take > skip ? take : skip;
12}
1int lis_dp(int *nums, int n) {
2int dp[2000];
3int i;
4int j;
5int best = 0;
6if (n == 0) {
7return 0;
8}
9for (i = 0; i < n; i++) {
10dp[i] = 1;
11for (j = 0; j < i; j++) {
12if (nums[j] < nums[i] && dp[j] + 1 > dp[i]) {
13dp[i] = dp[j] + 1;
14}
15}
16if (dp[i] > best) {
17best = dp[i];
18}
19}
20return best;
21}