Skip to content

1140. Stone Game II #359

Answered by topugit
mah-shamim asked this question in Q&A
Aug 20, 2024 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

We need to use dynamic programming to find the maximum number of stones Alice can get if both players play optimally. Here's a step-by-step approach to develop the solution:

  1. Define the State and Transition:

    • Define a 2D DP array where dp[i][m] represents the maximum stones Alice can collect starting from pile i with a maximum pick limit m.
    • Use a prefix sum array to efficiently calculate the sum of stones in subarrays.
  2. Base Case:

    • If there are no stones left to pick, the score is 0.
  3. Recursive Case:

    • For each pile i and maximum allowed pick m, calculate the maximum stones Alice can collect by considering all possible moves (taking 1 to 2m piles).
  4. Transition Function:

    • For each po…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mah-shamim
Comment options

mah-shamim Aug 20, 2024
Maintainer Author

Answer selected by mah-shamim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants