Link: https://leetcode.com/problems/stone-game/
Solution:
Topics: DP, game theory
Intuition
These “optimal play” problems will typically be DP…it’s just a matter of setting up the recursion correctly…which is not an easy task at all. I eventually did find a solution but unfortunately it was not efficient enough to pass all the test cases. I missed a couple of key insights on my first attempt.
Basically, we need to know 3 things to set up the recursion properly:
-
the range of
piles
that we have to work with -
whose turn it is
-
the score (or a representation of it)
-
The range of
piles
can simply be passed asl, r
. -
This is hard to observe, but the player turn is actually implied with the range
l, r
. The arraypiles
is guaranteed to be of even length, so it stands to reason that if the length of the subarray in the rangel, r
is even, then it is Alice’s turn. If odd, it is Bob’s turn. -
The score is way more manageable if we can represent it as one number. It just so happens that we can. Alice can add to the balance, and Bob can subtract from it. A victory for Alice would leave the balance positive, victory for bob- negative.
Now we can set up our DP. The range of piles in propagated in l, r
variables, the player turn is computed with provided l, r
, and the score will be bubbled up in the return values, which makes the function cache-able.
Implementation
Review 1
Great intro to “optimal play” problems. This time around I came up with a much cleaner solution with a more canonical implementation.
Implementation