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
a
and b
here represent the maximum A
can get by picking the starting pot or the ending pot, respectively.
We're actually trying to maximize A-B
, but since B = TotalGold - A
, we're trying to maximize 2A - TotalGold
, and since TotalGold
is constant, we're trying to maximize 2A
, which is the same as A
, so we completely ignore the values of B
's picks and just work with A
.
The updated parameters in the recursive calls include B
picking as well - so coin[start]
represents A
picking the start, then B
picks the next one from the start, so it's start+2
. For the next call, B
picks from the end, so it's start+1
and end-1
. Similarly for the rest.
We're taking the min
, because B
will try to maximize it's own profit, so it will pick the choice that minimizes A
's profit.
But actually I'd say this solution is lacking a bit in the sense that it just returns a single value, not 'an optimal strategy', which, in my mind, would be a sequence of moves. And it also doesn't take into account the possibility that A
can't win, in which case one might want to output a message saying that it's not possible, but this would really be something to clarify with the interviewer.