Understanding a solution to find the best gold coin strategy

I am having trouble understanding the reasons for resolving this issue at CareerCup .

The game of pots with gold: two players A & B. In the line there are pots of gold, each of which contains several gold coins (players can see how many coins are in each gold pot - excellent information). They receive alternating moves in which the player can choose a bank from one of the ends of the line. The winner is the player who has more coins at the end. The goal is to "maximize" the number of coins collected by A, provided that B also plays optimally. And the game begins.

The idea is to find the optimal strategy that will ensure the victory of A, knowing that B also plays optimally. How would you do that?

In the end, I was asked to encode this strategy!

This was a question from a Google interview.

Proposed Solution:

function max_coin( int *coin, int start, int end ):
    if start > end:
        return 0

    // I DON'T UNDERSTAND THESE NEXT TWO LINES
    int a = coin[start] + min(max_coin(coin, start+2, end), max_coin(coin, start+1, end-1))
    int b = coin[end] + min(max_coin(coin, start+1,end-1), max_coin(coin, start, end-2))

    return max(a,b)

There are two specific sections that I do not understand:

  1. In the first line, why do we use the ranges [start + 2, end] and [start + 1, end - 1]? This is always leaving one jar of coin. Shouldn't it be [start + 1, end], because we took out the initial can with a coin?
  2. In the first line, why do we take a minimum of two results, and not a maximum?
  3. Since I do not quite understand why the two lines take a minimum and why we choose these specific ranges, I'm not quite sure what and aactually represent ?ab
+8
5

a b a, .

A-B, B = TotalGold - A 2A - TotalGold, TotalGold , 2A, , a, b picks a.

b, coin[start] a , b , start+2. b , start+1 end-1. .

min, b , , a.

, , , " ", , , . , a , , , -, .

+6

a b , start ( end).

, :

int a = coin[start] + min(max_coin(coin, start+2, end), max_coin(coin, start+1, end-1))

start, coin[start]. start+1 end. , . , , . ,

  • start+1, max_coin(coin, start+2, end)
  • end Ill gain max_coin(coin, start+1, end-1)

, .

, end.

.. . max_coin(coin, start+1, end-1) . , . , , , . memoization .

+13

, , .

3 - a b , , /

2 - , - / , ,

1 - - , ? , ( + 2, ). / , ( + 1, -1)

+2

, x, , , y. x+y, a , (x=coin[start]) , b , (x=coin[end]) .

, y.

( , ), , . y=min(best_strategy_front(), best_strategy_end()) - - , , .

, .

+1

. .

public class Problem08 {
  static int dp[][];

  public static int optimalGameStrategy(int arr[], int i, int j) {
    //If one single element then choose that.
    if(i == j) return arr[i];

    //If only two elements then choose the max.
    if (i + 1 == j ) return Math.max(arr[i], arr[j]);

    //If the result is already computed, then return that.
    if(dp[i][j] != -1) return dp[i][j];

    /**
     * If I choose i, then the array length will shrink to i+1 to j.
     * The next move is of the opponent. And whatever he choose, I would want the result to be
     * minimum. If he choose j, then array will shrink to i+1, j-1. But if also choose i then
     * array will shrink to i+2,j. Whatever he choose, I want the result to be min, hence I take
     * the minimum of his two choices.
     *
     * Similarly for a case, when I choose j.
     *
     * I will eventually take the maximum of both of my case. :)
     */
    int iChooseI = arr[i] + Math.min(optimalGameStrategy(arr, i+1, j-1),
        optimalGameStrategy(arr, i+2, j));
    int iChooseJ = arr[j] + Math.min(optimalGameStrategy(arr, i+1, j-1),
        optimalGameStrategy(arr, i, j-2));
    int res = Math.max(iChooseI, iChooseJ );
    dp[i][j] = res;
    return res;
  }


  public static void main(String[] args) {
    int[] arr = new int[]{5,3,7,10};
    dp = new int[arr.length][arr.length];
    for(int i=0; i < arr.length; i++) {
      for(int j=0; j < arr.length; j++) {
        dp[i][j] = -1;
      }
    }
    System.out.println( " Nas: " + optimalGameStrategy(arr, 0, arr.length-1));
  }
}
0

Source: https://habr.com/ru/post/1530224/


All Articles