Longest Increasing Subsequence visualization

For each index, the longest strictly increasing subsequence ending there.

dp[i] = 1 + max(dp[j]) over all j < i with A[j] < A[i]. The answer is the largest dp value; parent links rebuild the subsequence.

Edge cases: strictly increasing means equal values cannot both be used; a strictly decreasing array has LIS 1; an empty array has LIS 0.

Complexity

Time: best O(n²), average O(n²), worst O(n²). Space: O(n). Difficulty: Intermediate.

Pseudocode

procedure LIS(A):
  dp[i] ← 1 for all i
  for i ← 1 to n−1:
    for j ← 0 to i−1:
      if A[j] < A[i] and dp[j] + 1 > dp[i]:
        dp[i] ← dp[j] + 1; parent[i] ← j
  return max(dp), rebuilt via parent

More dynamic programming algorithms