This is a classical game where two players play following game:
There are n coins in a row with different denominations. In this game, players pick a coin from extreme left or extreme right (they blindly pick from any extreme with a probability of .5, both of them are dumb). I just want to count the expected sum of player who starts the game.
For this I want to sum up all the possible combinations of values a player can have. I am using a recursive solution which sums up all the possible outcome values but it is having overlapping sub-problems. I want to make it efficient and want to memoize these overlapping sub-problems.
I am not able to collect the logic to execute it. Please someone help.
Idea is for each row subinterval to store sums for both players.
Let
F(start, end)
denote possible sums of first player playing on interval[start, end]
. Similar defineS(start, end)
. We can store possible sums with a probabilities of sums with a dictionary, like{2: 0.25, 5: 0.25, 6: 0.5}
.Than recursions hold:
This can be calculated by increasing interval length:
Dictionaries
F(1, row_length)
andS(1, row_length)
are used to calculate expected sum.