dynamica programming coins in line

转载 2015年07月06日 23:16:33

There are n coins in a line. (Assume n is even). Two players take turns to take a coin from one of the ends of the line until there are no more coins left. The player with the larger amount of money wins.

  1. Would you rather go first or second? Does it matter?
  2. Assume that you go first, describe an algorithm to compute the maximum amount of money you can win.

Let us look one extra step ahead this time by considering the two coins the opponent will possibly take, Ai+1 and Aj. If the opponent takes Ai+1, the remaining coins are { Ai+2 … Aj }, which our maximum is denoted by P(i+2, j). On the other hand, if the opponent takes Aj, our maximum is P(i+1, j-1). Since the opponent is as smart as you, he would have chosen the choice that yields the minimum amount to you.

Therefore, the maximum amount you can get when you choose Ai is:

P1 = Ai + min { P(i+2, j), P(i+1, j-1) }

Similarly, the maximum amount you can get when you choose Aj is:

P2 = Aj + min { P(i+1, j-1), P(i, j-2) }


P(i, j) = max { P1, P2 }
        = max { Ai + min { P(i+2, j),   P(i+1, j-1) },
                Aj + min { P(i+1, j-1), P(i,   j-2) } }

Although the above recurrence relation could be implemented in few lines of code, its complexity is exponential. The reason is that each recursive call branches into a total of four separate recursive calls, and it could be n levels deep from the very first call). Memoization provides an efficient way by avoiding re-computations using intermediate results stored in a table. Below is the code which runs in O(n2) time and takes O(n2) space.

Updated code with a new function printMoves which prints out all the moves you and the opponent make (assuming both of you are taking the coins in an optimal way).

【LintCode】Coins in a Line 硬币排成行

有 n 个硬币排成一条线。两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止。拿到最后一枚硬币的人获胜。 请判定 第一个玩家 是输还是赢?样例 n = 1, 返回 true. n...
  • wutingyehe
  • wutingyehe
  • 2015年07月15日 09:52
  • 1191

lintcode Coins in Line II 硬币排成线 II

硬币排成线 II  描述 笔记  数据  评测 有 n 个不同价值的硬币排成一条线。两个参赛者轮流从左边依次拿走 1 或 2 个硬币,直到没有硬币为...
  • u014470581
  • u014470581
  • 2016年10月27日 11:21
  • 414

lintcode coins-in-a-line 硬币排成线

问题描述笔记设置buff[i]为:有i个硬币的时候第一个玩家是赢还是输。 如果buff[i-1]和buff[i-2]都是赢,那我一开始无论取1个还是2个,对方都可以赢。(我取完之后可以看做对方是先手)...
  • waltonhuang
  • waltonhuang
  • 2016年07月26日 23:14
  • 367

ACM-ICPC 2017 Asia Urumqi: K. Sum of the Line(容斥+莫比乌斯)

Consider a triangle of integers, denoted by T. The value at (r,c) is denoted by Tr,c​, where 1≤r and...
  • Mitsuha_
  • Mitsuha_
  • 2018年02月10日 21:38
  • 104

SCU 4489 misaka and last order(暴力枚举)

misaka and last order Description Misaka Mikoto is a main character of the Animation "To Aru Maju...
  • blessLZH0108
  • blessLZH0108
  • 2017年03月18日 16:20
  • 269

CVX: Matlab Software for Disciplined Convex Programming

http://cvxr.com/cvx/ CVX is a Matlab-based modeling system for convex optimization. CVX turns Matla...
  • BingeCuiLab
  • BingeCuiLab
  • 2015年06月25日 16:23
  • 1109

**[Lintcode]Coins in a Line

There are n coins in a line. Two players take turns to take one or two coins from right side until t...
  • jc69186918
  • jc69186918
  • 2016年08月28日 10:14
  • 217

coins in a line

有 n 个硬币排成一条线。两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止。拿到最后一枚硬币的人获胜。 请判定 第一个玩家 是输还是赢? 您在真实的面试中是否遇到过这...
  • gettogetto
  • gettogetto
  • 2015年11月10日 13:17
  • 147

#394 Coins in a Line

题目描述: There are n coins in a line. Two players take turns to take one or two coins from right...
  • haifischxia
  • haifischxia
  • 2016年08月19日 12:44
  • 62

Coins in a Line

题目来源: http://leetcode.com/2011/02/coins-in-line.html 二维DP。代码如下:
  • zhaobaoxue
  • zhaobaoxue
  • 2014年10月29日 21:29
  • 279
您举报文章:dynamica programming coins in line