Type like pro

Index

Gold coins in pots game

Gold coins in pots game

Problem

Some pots filled with gold coins and arranged in a line. There are different number of gold coins in different pots. The number is mentioned on each of the pots. Two players will take turn alternatively. In each turn a player can pick any one the two pots at the two ends of the line. Find the winning strategy for a player getting chance to play first, assuming the opponent player is also playing optimally. How many coins will the first player get if he plays optimally?

Solution

Suppose the pots are arranged in L[0] to L[n]. First player can pick either L[0] or L[n]. If he picks L[0] Second player can pick either L[1] or L[n]. If first picks L[n], second player can pick L[0] or L[n-1]. As the second player is also playing optimally he will also use the same strategy as the first player. Let us assume f(i,j) is the number of coins a player would get if he plays optimally from a point when two ends are i and j (0<=i<=j<=n). So f(0,n) would be the number of coins that first player would get after playing optimally. As second player is also playing optimally, he will pick a pot such that after his picking first player will get fewer gold if played optimally. If second player takes pot number 1, player 1 will get f(2,n). If second player takes pot number n, first player will get f(1,n-1). Second player will play such that 1st player gets minimum of these two options. So if first player takes pot 0 his final count of gold will be L[0]+Min(f(2,n),f(1,n-1)). Similarly if he picks the last pot this total count will be L[n]+Min(f(0,n-2),f(1,n-1)). From these two items whichever is greater First player should pick that, and that should be his winning strategy as playing first.

This is implemented with dynamic programming as we can see optimal subproblem and recurring subproblem. A 2 dimensional array is used to memoize the function output. So L[i][j]=f(i,j). We will initialize the array elements only once. So the complexity will be O(n^2).


Code

public class PotsOfGoldGame
{
 public static void main(String[] args)
 {
  int[] goldPots =
  { 12, 32, 4, 23, 6, 42, 16, 3, 85, 23, 4, 7, 3, 5, 45, 34, 2, 1 };
  int coins = getMaxGold(goldPots);
  System.out.println(coins);
 }

 private static int getMaxGold(int[] goldPots)
 {
  Integer[][]memo=new Integer[goldPots.length][goldPots.length];
  return getMaxGold(goldPots, 0, goldPots.length - 1,memo);
 }

 private static int getMaxGold(int[] goldPots, int startIndex, int endIndex,Integer[][]memo)
 {
  if (startIndex > endIndex)
   return 0;
  if(memo[startIndex][endIndex]!=null)
   return memo[startIndex][endIndex];
  int coinsIfStart = goldPots[startIndex]
    + Math.min(getMaxGold(goldPots, startIndex + 2, endIndex,memo),
      getMaxGold(goldPots, startIndex + 1, endIndex - 1,memo));
  int coinsIfEnd = goldPots[endIndex]
    + Math.min(getMaxGold(goldPots, startIndex, endIndex - 2,memo),
      getMaxGold(goldPots, startIndex + 1, endIndex - 1,memo));
  memo[startIndex][endIndex]=Math.max(coinsIfStart, coinsIfEnd);
  return memo[startIndex][endIndex];
 }
}