cconnected.
Understand the pattern. Find your own way.Find your starting point
Your learning feed/Idea 133 of 218
Ideas this builds on
Advanced DP patterns133 / 218

Game DP: describe the advantage

Picture

From [1,5,2], taking 2 leaves the opponent [1,5]. Immediate value alone does not decide the winner.

Predict

Why subtract the smaller-game answer?

Walk through the small example

The other player becomes current. Zero-sum turns two players’ totals into one signed advantage.

Mental model and complete Python solution

Instead of tracking two scoreboards, track “how far ahead can the player to move finish?” After taking a value, the opponent owns the smaller game’s advantage, so subtract it.

What the function promises: Return the first player’s maximum final score minus the second’s. Empty input returns zero.

def score_advantage(values):
    n = len(values)
    if not n: return 0
    dp = list(values)
    for length in range(2, n+1):
        for left in range(n-length+1):
            right = left+length-1
            dp[left] = max(values[left]-dp[left+1], values[right]-dp[left])
    return dp[0]

assert score_advantage([1,5,2]) == -2
assert score_advantage([1,5,233,7]) == 222

Work and storage: O(n²) time and O(n) space.

When to recognize it: Zero-sum turns two players’ totals into one signed advantage.

Check before applying it: This recurrence assumes perfect play, alternating turns, and endpoint-only moves.

Change one rule

The game is not zero-sum.

See what changes

One difference may not summarize both players’ objectives; keep the relevant payoff pair or use game-specific equilibrium reasoning.

Connect

interval dp: Each move shrinks one boundary.

greedy counterexamples: Largest immediate endpoint can lose after the opponent responds.