Playing optimally, for this game, players will receive points. Let the input be a list L and define F(i,l) and S(i,l) as the first and second player ratings for the part of the list, starting from the ith element with a length l. If (part) of the list has a length of 1, then this card will go to the first player.
F(i,1) = L[i] S(i,1) = 0
If (part) of the list is longer than the first player wants to maximize the amount of the card that he chooses, and that he will receive as the second player with the list of remaining ones.
F(i,l) = max( L[i] + S(i+1,l-1), L[i+l-1] + S(i,l-1) )
Since the second player does not select a card, he will receive the fact that the first player is on the shorter list.
S(i,l) = F(i+1,l-1) if L[i] + S(i+1,l-1) >= L[i+l-1] + S(i,l-1), or F(i,l-1) if L[i] + S(i+1,l-1) < L[i+l-1] + S(i,l-1).
The implementation is performed by filling in two arrays n^2 for F and S The result is F(1, length(L) ) .
source share