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].
Brute force approach
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.
At most 4 values. 10 9 2 5 has length 2.
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.
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
Pseudocode & Complexity
| Operation | Time |
|---|---|
| Pair (j, i) | O(1) |
| All pairs | O(n²) |
| Space | O(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