Pots of Gold Game using Dynamic Programming

In Pots of gold game, there are two players A & B and pots of gold arranged in a line, each containing some gold coins. The players can see how many coins are there in each gold pot and each player gets alternating turns in which the player can pick a pot from one of the ends of the line. The winner is the player which has a higher number of coins at the end. The objective is to “maximize” the number of coins collected by A, assuming B also plays optimally and A starts the game.

The idea is to find an optimal strategy that makes player wins knowing that opponent is playing optimally as well. The player has two choices for coin[i..j] where i and j denotes the front and rear of the line respectively.

If the player chooses front pot i, opponent is left to choose from [i+1, j].

Since opponent is playing optimally as well, he will try to minimize the points collected by the player. i.e. opponent will make a choice that will leave the player with minimum coins. So the problem can be recursively defined as –

The time complexity of above solution is exponential and auxiliary space used by the program is O(1).

The problem has an optimal substructure. We have seen that the problem can be broken down into smaller subproblems which can further be broken down into yet smaller subproblems, and so on. The problem also exhibits overlapping subproblems so we will end up solving the same subproblem over and over again. We know that problems having optimal substructure and overlapping subproblems can be solved by dynamic programming, in which subproblem solutions are Memoized rather than computed again and again. This method is illustrated below –