Sub topic 7
Shrinking from the Ends
This chapter assumes you already know how dp[i][j] is defined over an inclusive range, what the base cases for empty and single-element ranges look like, and why tables are filled by increasing interval length. Here you will learn the second main recurrence shape in interval DP.
Two Shapes of Recurrence
A split-point recurrence picks some k inside [i, j] and joins [i, k] with [k+1, j]. That loop over k costs O(n) per state.
An endpoint-shrinking recurrence never looks inside the range. It only asks what happens at the two ends, i and j, and then moves to a range that is one or two elements shorter:
dp[i+1][j]: drop the left end.dp[i][j-1]: drop the right end.dp[i+1][j-1]: drop both ends.
Each state reads a fixed number of neighbors, so the transition costs O(1). The whole table then costs O(n²) time rather than O(n³).
When This Shape Fits
Use endpoint shrinking when the decision about range [i, j] depends only on its outermost elements. Common signals:
- Elements are removed or taken from either end, as in two-player games where each player picks from the left or the right.
- You compare
s[i]withs[j], as in palindrome problems. - Matching the two ends lets you "peel" them off together.
If the best answer depends on where the range is cut in the middle, you need a split point instead.
Worked Example: Picking from Either End
Two players take turns removing one number from the left or right end of an array. Each player adds the number they take to their own score, and both play optimally. Define dp[i][j] as the largest score difference (current player minus opponent) that the player to move can guarantee on nums[i..j], inclusive.
If the player takes nums[i], the opponent then moves on [i+1, j] and gains dp[i+1][j] relative to the first player. Taking nums[j] works the same way, with [i, j-1]:
def first_player_margin(nums):
n = len(nums)
if n == 0:
return 0
dp = [[0] * n for _ in range(n)]
for i in range(n - 1, -1, -1):
dp[i][i] = nums[i]
for j in range(i + 1, n):
take_left = nums[i] - dp[i + 1][j]
take_right = nums[j] - dp[i][j - 1]
dp[i][j] = max(take_left, take_right)
return dp[0][n - 1]
print(first_player_margin([1, 5, 2])) # -2The base case is a single element: dp[i][i] = nums[i], because the player to move simply takes it.
Evaluation Order Without a Length Loop
You can still fill by length. However, endpoint recurrences allow a simpler order: i from n-1 down to 0, and j from i+1 up to n-1. When the code computes dp[i][j]:
| Dependency | Why it is ready |
|---|---|
| `dp[i+1][j]` | Row `i+1` was finished before row `i` |
| `dp[i][j-1]` | Same row, smaller `j`, already computed |
| `dp[i+1][j-1]` | Row `i+1`, finished earlier |
The Empty-Range Trap
When the "drop both ends" case reaches a length-2 range, it reads dp[i+1][j-1] with i+1 > j-1. That cell is the empty range. It must hold the identity value for your problem (often 0), and a zero-initialized table supplies it automatically. If your empty value is not 0, for example -inf for an infeasible range, handle i + 1 > j - 1 explicitly. The next chapter uses this case directly.
Exercise
Run firstplayermargin by hand on [3, 9, 1, 2]. What is dp[0][3], and which end should the first player take on the first move?
Show answer
The single cells are 3, 9, 1, 2. For length 2: dp[0][1]=6, dp[1][2]=8, dp[2][3]=1. For length 3: dp[0][2]=max(3-8, 1-6)=-5 and dp[1][3]=max(9-1, 2-8)=8. For the full range, dp[0][3]=max(3-8, 2-(-5))=7. The first player takes the 2 on the right and wins by 7.
Exercise
Why does this recurrence run in O(n^2) time, while matrix chain multiplication runs in O(n^3)?
Show answer
The table has O(n^2) states in both problems. Here each state reads at most three neighbors, so the work per state is O(1). Matrix chain tries every split point k, which costs O(n) per state.