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!
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:
a
b
a b a, .
A-B, B = TotalGold - A 2A - TotalGold, TotalGold , 2A, , a, b picks a.
A-B
B = TotalGold - A
2A - TotalGold
TotalGold
2A
b, coin[start] a , b , start+2. b , start+1 end-1. .
coin[start]
start+2
start+1
end-1
min, b , , a.
min
, , , " ", , , . , a , , , -, .
a b , start ( end).
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. , . , , . ,
max_coin(coin, start+2, end)
max_coin(coin, start+1, end-1)
, .
, end.
.. . max_coin(coin, start+1, end-1) . , . , , , . memoization .
, , .
3 - a b , , /
2 - , - / , ,
1 - - , ? , ( + 2, ). / , ( + 1, -1)
, x, , , y. x+y, a , (x=coin[start]) , b , (x=coin[end]) .
x
y
x+y
x=coin[start]
x=coin[end]
, y.
( , ), , . y=min(best_strategy_front(), best_strategy_end()) - - , , .
y=min(best_strategy_front(), best_strategy_end())
. .
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)); } }
Source: https://habr.com/ru/post/1530224/More articles:Duplicating terminal output from a Python subprocess - python-Name of the start-field when using the assigned initializers - cStraight scroll compatible with Lazy loading - jsf-2Confused about ContentType - djangoOracle Database connection through SQLPLUS - oracleReplica-set Mongodump: how to force on a secondary? - mongodbWhat is the best practice for placing the icon to the right of the text input field - htmlНевозможно получить легкие миниатюры и не усердно работать с Mac, не создаются ошибки - pythonError switching FragmentActivity ... "Incompatible types" or "FragmentTransaction cannot be applied" - androidHow to use the tip of a tool in a line chart using D3 js - d3.jsAll Articles