##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
You've optimized single-player decisions. Now handle two players taking turns. Solve Stone Game variants where both sides play to win.
Two players, optimal play
Alternating turns, perfect info
LC 1510 - remove square amounts
Winning if opponent can lose
dp[n] = can current player win?
Try all squares, find losing state
dp[0]=false, dp[1]=true, dp[2]=false
O(n√n) checking squares
√n moves per position
LC 1406 - take 1, 2, or 3
Maximize score difference
dp[i] = advantage from position i
Take stones, subtract opponent's best
dp[n] = 0, no stones left
Right to left, O(n)
O(1) space with sliding window
XOR piles, zero = lose
XOR trick explained
Grundy = mex of reachable states
mex = minimum excludant
XOR Grundy for combined games
Start leaning basics.
Start leaning basics.
Odd steps only matter
Even-to-odd = adding to Nim
XOR Grundy numbers
mex of reachable Grundy values
Topological order on DAG
LC 877 - pick from ends
Interval DP or math trick
Alice always wins (odd total)
LC 1690 - score = remaining sum
Remove stone, score the rest
Prefix sums for range totals
Which game pattern is this?
Single pile, symmetric games
Whose turn? Score difference handles it
LC 913
Solution approach
LC 1872
Solution approach
CSES 2207
Solution approach
From win/lose to Grundy