LintCode 394. Coins in a Line (博弈类DP 经典题)

  1. 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 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?

If the first player wins, return true, otherwise return false.

Example
Example 1:

Input: 1
Output: true
Example 2:

Input: 4
Output: true
Explanation:
The first player takes 1 coin at first. Then there are 3 coins left.
Whether the second player takes 1 coin or two, then the first player can take all coin(s) left.
Challenge
O(n) time and O(1) memory

解法1:
经过分析可知,当n为3的倍数时,先手肯定输,否则先手肯定赢。
为什么呢?当n为3的倍数时,先手不管怎么取(取1个还是2个),后手都有办法让剩下的coin为3的倍数。最后当轮到先手时,剩下的coin为3,先手肯定输了。
当n不为3的倍数时,先手可以通过取1个或2个让剩下的coin为3的倍数。这样后手肯定输。比如说n=5,先手取2个,剩下3个,后手肯定输。

代码如下:

class Solution {
public:
    /**
     * @param n: An integer
     * @return: A boolean which equals to true if the first player will win
     */
    bool firstWillWin(int n) {
        return n % 3;
    }
};

解法2:
用DP。参考
http://www.cnblogs.com/grandyang/p/5861500.html
代码如下:

class Solution {
public:
    /**
     * @param n: an integer
     * @return: a boolean which equals to true if the first player will win
     */
     bool firstWillWin(int n) {
        if (n <= 0) return false;
        if (n <= 2) return true;
        vector dp(n + 1, true);
        dp[3] = false;
        for (int i = 4; i <= n; ++i) {
            dp[i] = dp[i - 3];
        }
        return dp.back();
    }
};

你可能感兴趣的:(LintCode 394. Coins in a Line (博弈类DP 经典题))