You are on page 1of 3

Problem 2:

Assuming that both players are trying to optimize their final value, the optimal strategy for the
game would be as follows:

Alice is going first. Both Alice and Bob will sum up the values of every other number in
alternating sequences, that is, the sum of all odd-positioned values and the sum of all even-
positioned values. Now we consider a few cases:

If there are an even number of values, Alice’s strategy is to simply pick the end that belongs to
the higher summation. This guarantees that Alice can maximize her score. The reason why this
strategy works is because we are assuming that both players are playing optimally, and so if Bob
were to see, based on the optimal strategy, that he couldn’t win the game, he would try as best as
possible to maximize his own score, which means that he must play the same way as Alice and
select the other sequence of alternating values. This shows that Alice is able to maximize her
final score if she simply starts by choosing the value that contributes to the larger of the two
sums; since Bob will always pick from the other sequence to maximize his score, Alice is able to
select all numbers that contribute to the larger sum.

If there are an odd number of values, Alice’s strategy is slightly different:


If the larger summation is made up of the odd-positioned values, Alice has to pick one of the
values on either end, then by the optimal strategy, Bob will end up picking all the other values
that sum to the larger amount, leaving Alice with the other values. If the larger summation is
made up of the even-positioned values, Alice will have to pick one of the end values, and Bob
will still end up choosing all the values that add up to the larger sum. However, both of these
scenarios is ok, because the problem simply asks for us to maximize Alice’s score, not see if she
can win in each scenario.

The algorithm for this problem would look like this:


Let vi be the ith value in the row
Given n values in a row:

Initialize sum1 and sum2 to 0


For x from 1 to n (x is the index, and this row is 1-indexed)
If x is odd
Sum1 = sum1 + vx
Else
Sum2 = sum2 + vx
End
(This is so that both players can determine their optimal strategy)

Assume each iteration occurs on the start of alice’s turn


Initialize count to n
While n > 0
If count is odd:
If count = 1
Alice picks the last number.
Else
Arbitrarily select either end value
(Bob selects from the larger-sum sequence of values at this moment)
Else
If (sum1 > sum2)
Select the odd-indexed end value, add it to Alice’s total
Else
Select the even-indexed end value, add it to Alice’s total
(in both cases, Bob picks from the other sequence to maximize his own score. It
doesn’t matter from which end he picks his next value. Add this value to Bob’s total)
Set count to count - 2
End
Return Alice’s total

This algorithm runs in O(n) time because our for loop iterates through all the numbers once, and
our while loop adds all the numbers to either player’s total once as well.

Correctness: To prove our algorithm works, we assume the following claim:

The optimal solution does not give the maximum value.


If this were the case, then that means we can swap out a value from Alice’s optimal solution set
and replace it with a higher value from the other set (if this is possible). In order for this to work,
Alice would’ve had to select the value from the other set instead, and Bob would have had to
select the replaced value instead as well. But this means that Bob wouldn’t be playing optimally,
because he wouldn’t have selected this value, as it would’ve belonged to Alice’s sequence of
values, and not his sequence. Thus, we’ve shown that the maximum value can’t be improved
without violating the fact that both players are using the optimal strategy.

You might also like