2
def stoneGameII(self, piles: List[int]) -> int:6
def recursion(index, M):7
# if we reached to the end we cannot score any value10
# we search if we have solved the same case earlier13
# total remaining score is the sum of array from index to the end14
total = sum(piles[index:])15
# if we can take the complete array it is the best choice16
if index + 2 * M >= n:18
# my_score is the score we are getting as the player who is playing20
for x in range(index, index + 2 * M):21
# opponent score will be calculated by next recursion22
opponent_score = recursion(x + 1, max(M, x - index + 1))23
# my_score is the remaining value of total - opponent_score24
my_score = max(my_score, total - opponent_score)25
# this is memoization part26
dp[(index, M)] = my_score30
return recursion(0, 1)