Sub topic 2
Base Cases: Empty and Single-Element Ranges
This chapter assumes you know basic tabulation and memoization, and that you have already defined an interval state dp[i][j] with clear bounds. Every interval recurrence keeps shrinking ranges until they become too small to split or trim. Those smallest ranges are the base cases. If you get them wrong, every larger answer built on them is wrong too.
Two Kinds of Smallest Range
With inclusive bounds, dp[i][j] covers elements i through j. The smallest ranges are:
| Range | Condition | Meaning |
|---|---|---|
| Single element | `i == j` | One item, no choice left |
| Empty | `i > j` | No items at all |
The value of each base case comes from the problem itself, not from a fixed rule. Ask yourself: "What is the correct answer for one item? For zero items?"
| Problem | `dp[i][i]` | Empty range |
|---|---|---|
| Matrix chain cost | `0` (one matrix, no multiplication) | never reached |
| Longest palindromic subsequence | `1` (a single character is a palindrome) | `0` |
| Merging adjacent piles | `0` (one pile, nothing to merge) | never reached |
When Empty Ranges Appear
Split-point recurrences divide [i, j] into [i, k] and [k+1, j] with i <= k < j. Both halves always hold at least one element, so empty ranges never come up and dp[i][i] is the only base case.
Endpoint-shrinking recurrences are different. When s[i] == s[j] and j == i + 1, the recurrence reads dp[i+1][j-1], which is dp[i+1][i], an empty range. Its value must be 0. A zero-initialized table gives you that for free, because the loop never writes to cells below the diagonal.
def lps_length(s):
n = len(s)
if n == 0:
return 0
dp = [[0] * n for _ in range(n)] # cells with i > j stay 0: empty ranges
for i in range(n - 1, -1, -1):
dp[i][i] = 1 # single-element base case
for j in range(i + 1, n):
if s[i] == s[j]:
dp[i][j] = dp[i + 1][j - 1] + 2 # reads an empty range when j == i + 1
else:
dp[i][j] = max(dp[i + 1][j], dp[i][j - 1])
return dp[0][n - 1]
print(lps_length("aa")) # 2
print(lps_length("ab")) # 1Exclusive Bounds Shift the Base Case
Some problems read more cleanly with open intervals (left, right), where the endpoints are fixed boundaries and are not part of the range. Then the empty range is right == left + 1, and that is the natural base case. Burst balloons, covered later, uses this form: with no balloons strictly between left and right, the score is 0.
Common Errors
- Mixing bound conventions. You write the recurrence with inclusive bounds but set base cases as if they were exclusive. The result is off by one everywhere. Choose one convention and write it in a comment above the table.
- Forgetting the length-2 case. With an endpoint recurrence, a length-2 range reads an empty range. Test two-element inputs on purpose.
- Negative indices in Python.
dp[i+1][j-1]withj = 0readsdp[i+1][-1], which silently wraps to the last column. Make sure your loop bounds never produce that index. - Wrong identity value. Use
0for counts and costs,1for products, and infinity for impossible states. Put a placeholder in the wrong place and it can win aminormax.
Exercise
You define dp[i][j] as the minimum cost to merge piles i through j inclusive, using the split recurrence dp[i][j] = min over i <= k < j of dp[i][k] + dp[k+1][j] + sum(i..j). What should dp[i][i] be, and do you need a value for i > j?
Show answer
dp[i][i] = 0, because a single pile needs no merge. You don't need a value for i > j: with i <= k < j, both halves [i, k] and [k+1, j] always contain at least one pile, so an empty range is never read.
Exercise
Under open bounds (left, right), what is the base case condition, and why would using left == right as the base case be a mistake?
Show answer
The base case is right == left + 1, meaning no elements lie strictly between the boundaries. With left == right, the range would hold a negative number of elements, so the recurrence never reaches that state. The real empty ranges would then fall into the recursive branch with an empty loop and depend on whatever default value the loop leaves behind.