Stage 1 · Code
Dynamic Programming
Longest Increasing Subsequence
O(n²) DP and O(n log n) patience sorting approach.
O(n²) DP Solution
dp[i] = length of LIS ending at index i. For each i, check all j < i: if nums[j] < nums[i], dp[i] = max(dp[i], dp[j] + 1). Answer = max(dp).
O(n log n) Patience Sorting
Maintain array tails where tails[len] = smallest ending value of all increasing subsequences of length len+1. For each num, find insertion point with binary search. Length of tails = LIS length.
Reconstructing LIS
O(n²) DP: track parent pointers. O(n log n): track index of each element in tails, and predecessor index. Reconstruct by following parents from last element of LIS.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.