[LintCode] 394. Coins in a Line_ Medium tag:Dynamic Programming_博弈

本文介绍了一个经典的博弈论问题——取硬币游戏。通过动态规划的方法,探讨了如何决定先手玩家是否能赢得游戏。文章提供了两种解决方案,并最终简化为一个简洁的判断条件。

Description

There are n coins in a line. Two players take turns to take one or two coins from right side until there are no more coins left. The player who take the last coin wins.

Could you please decide the first player will win or lose?

Example

n = 1, return true.

n = 2, return true.

n = 3, return false.

n = 4, return true.

n = 5, return true.

Challenge

O(n) time and O(1) memory

 

这个题目是属于经典的博弈类Dynamic Programming 的入门题目, 为了便于思考, 我们只考虑一个选手的status, 例如我们只考虑first player的状态, 然后 A[i] 表明到first player下时还

剩下i个coins, 找到动态关系式:    A[i] = (A[i-2] and A[i-3]) or (A[i-3] and A[i-4])    第一个是first player只选一个, 第二个是first player选2个之后由second player选了之后的到first player

下了的状态, 所以将second player的状态放入到了first player的状态当中. 此时需要注意的是初始化dp数组时, 要初始化0, 1,2, 3, 4

 

1. Constraints

1) n >= 0 integer

 

2. ideas

DP    T: O(n)   S; O(1)   optimal by rolling array

 

3. code

1) S: O(n)

class Solution:
    def coinsInLine(self, n):
        ans = [True]*5
        ans[0] = ans[3] = False
        if n < 5: return ans[n]
        ans = ans + [None]*(n-4)
        for i in range(5, n+1):
            ans[i] = (ans[i-2] and ans[i-3]) or (ans[i-3] and ans[i-4])
        return ans[n]

 

2) S: O(1)

 

class Solution:
    def coinsInLine(self, n):
        ans = [True] *5
        ans[0] = ans[3] = False
        if n < 5: return ans[n]
        for i in range(5, n + 1):
            ans[i%5] = (ans[i%5-2] and ans[i%5-3]) or (ans[i%5-3] and ans[i%5-4])
        return ans[n%5]

 

3) 666

class Solution:
    def coinsInLine(self, n):
        return not (n%3 == 0)

 

4. Test cases

1) 0-6

 

转载于:https://www.cnblogs.com/Johnsonxiong/p/9368949.html

{ "amount": 0, "datt": { "wxu_titelimg": "images/logo0.png", "wxu_shopmid": 0, "wxu_lasttime": 1747753533900, "wxu_password": "e10adc3949ba59abbe56e057f20f883e", "wxu_agent_back": 0, "wxu_agent": 0, "wxu_shop_point": 0, "wxu_traffic": 0, "wxu_show": 0, "wxu_amount": 0, "wxu_pay_oldpay": 0, "wxu_agent_type": 0, "wxu_opid": "ok8aw6Y4Ex7CakQtswLE9i8MQpsI", "wxu_type": 1, "wxu_remark5": "1", "wxu_remark6": "266", "wxu_pay_oldpoint": 0, "wxu_headimgurl": "", "wxu_shop_cashmid": 0, "wxu_agent_rate": "0.60", "wxu_orcount": 0, "wxu_mid": 26923, "wxu_suppysubid": "0", "wxu_cashpass": "e10adc3949ba59abbe56e057f20f883e", "wxu_shop_cash": 0, "wxu_usertype": 0, "wxu_card": 0, "wxu_treeid": "", "wxu_country": "", "wxu_suppyid": "55", "wxu_otptype": 0, "wxu_pay_level": 10, "wxu_rate": 6, "wxu_clicktime": 1747753533897, "wxu_nickname": "", "wxu_lod_url": "BackMain01", "wxu_city": "", "wxu_sex": "0", "wxu_province": "", "wxu_showtitel": "样样通", "wxu_oldamount": 0, "wxu_shop_pointmid": 0, "wxu_traffic_type": 0 }, "tickets": 0, "data": { "wxu_titelimg": "images/logo0.png", "wxu_shopmid": 0, "wxu_lasttime": 1747753533900, "wxu_password": "e10adc3949ba59abbe56e057f20f883e", "wxu_agent_back": 0, "wxu_agent": 0, "wxu_shop_point": 0, "wxu_traffic": 0, "wxu_show": 0, "wxu_amount": 0, "wxu_pay_oldpay": 0, "wxu_agent_type": 0, "wxu_opid": "ok8aw6Y4Ex7CakQtswLE9i8MQpsI", "wxu_type": 1, "wxu_remark5": "1", "wxu_remark6": "266", "wxu_pay_oldpoint": 0, "wxu_headimgurl": "", "wxu_shop_cashmid": 0, "wxu_agent_rate": "0.60", "wxu_orcount": 0, "wxu_mid": 26923, "wxu_suppysubid": "0", "wxu_cashpass": "e10adc3949ba59abbe56e057f20f883e", "wxu_shop_cash": 0, "nickname": "", "headimgurl": "", "wxu_usertype": 0, "wxu_card": 0, "wxu_treeid": "", "wxu_country": "", "wxu_suppyid": "55", "wxu_otptype": 0, "wxu_pay_level": 10, "wxu_rate": 6, "wxu_clicktime": 1747753533897, "wxu_nickname": "", "wxu_lod_url": "BackMain01", "wxu_city": "", "wxu_sex": "0", "wxu_province": "", "wxu_showtitel": "样样通", "wxu_oldamount": 0, "wxu_shop_pointmid": 0, "wxu_traffic_type": 0 }, "coins": 0, "success": true, "data3": [], "baramount": 0, "coinlabel": "币" }
07-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值