Please check out this problem I have:
"You and your eight-year-old nephew Elmo decide to play a simple card game. At the beginning of the game, the cards are dealt face up in a long row. Each card is worth a different number of points. After all the cards are dealt, you and Elmo take turns removing either the leftmost or rightmost card from the row, until all the cards are gone. At each turn, you can decide which of the two cards to take. The winner of the game is the player that has collected the most points when the game ends. Having never taken an algorithms class, Elmo follows the obvious greedy strategy?when it?s his turn, Elmo always takes the card with the higher point value. Your task is to find a strategy that will beat Elmo whenever possible. (It might seem mean to beat up on a little kid like this, but Elmo absolutely hates it when grown-ups let him win.)
Describe and analyze an algorithm to determine, given the initial sequence of cards, the maximum number of points that you can collect playing against Elmo."
I've already done most of the theorical work in this problem. For instance, I've done the optimus substructure demonstration which is needed for DP, and I have defined the Recursive inefficient form, that explains how the game gets done. Now the next step is to design a bottom-up algorithm that solves this problem efficiently or, if it might help, a top-down memoization solution. I just can't do any of them. How would you solve this problem?
The algorithm is simple and you can use memoization and dynamic programming in this way:
def findMax(mem, cards, myTurn):
maxValue = 0
if(not cards):
return 0
if str(cards) in mem: #If we have already compute this state
return mem[str(cards)]
elif not myTurn: #turn of Elmo
if cards[0] > cards[len(cards) - 1]:
maxValue = findMax(mem, cards[1:], True)
else:
maxValue = findMax(mem, cards[:-1], True)
else: #your turn
maxValue = max(cards[0] + findMax(mem, cards[1:], False), cards[len(cards) - 1] + findMax(mem, cards[:-1], False))
mem[str(cards)] = maxValue #Store the max value for this state
return maxValue
import random
size = int(10 + random.randint(0,1))
cards = [random.randint(0,50) for x in range(size)]
print "Cards" + str(cards)
print findMax({}, cards, True)
Output:
Cards: [28, 33, 48, 0, 26, 1, 3, 11, 22, 32, 12]
Max value: 120