I am having trouble understanding the reasoning behind the solution to this question on CareerCup.
Pots of gold game: Two players A & B. There are pot
Assume what you gain on your turn is x
and what you get in all consequent turns is y
. Both values represent x+y
, where a
assumes you take next pot (x=coin[start]
) from the front and b
assumes you take your next pot (x=coin[end]
) from the back.
Now how you compute y
.
After your choice, the opponent will use the same optimum strategy (thus recursive calls) to maximise his profit, and you will be left with a the smaller profit for the turn. This is why your y=min(best_strategy_front(), best_strategy_end())
-- your value is the smaller of the two choices that are left because the opponent will take the bigger.
The indexing simply indicates the remaining sequences minus one pot on the front and on the back after you made your choice.