Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 486 Predict the Winner - State Design

Dp[i][j] for range

dp[i][j]dp[i][j] = the maximum score difference the current player can achieve on subarray nums[i..j][i..j]. Positive means current player wins on this subarray, negative means they lose. Base case: dp[i][i]=nums[i]dp[i][i] = \text{nums}[i]. Only one element left. Current player takes it, and the difference equals that value since opponent gets nothing from this subarray.

This is interval DP: we compute answers for smaller ranges first, then build up to larger ranges. Process subarrays of length 11, then 22, then 33, and so on. The final answer is dp[0][n1]dp[0][n-1].