LintCode "Coins in a Line"

Recursion + Memorized Search(DP). And apparently, the code below can be iterative with only 3 vars - DP.

class Solution {
    unordered_map<int, bool> cache;
public:
    bool go(int n)
    {
        if (n <=0) return false;
        if (n < 3) return true;

        if(cache.find(n) == cache.end())
        {
            cache[n] = !go(n - 1) || !go(n - 2);
        }
        return cache[n];
    }
    /**
     * @param n: an integer
     * @return: a boolean which equals to true if the first player will win
     */
     bool firstWillWin(int n) {
        return go(n);
    }
};
时间: 2024-10-11 00:42:31

LintCode "Coins in a Line"的相关文章

[LintCode] Coins in a Line III

Coins in a Line III There are n coins in a line. 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. Could you please decide the first player will

[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 there are no more coins left. The player who take the last coin wins. Could you please decide the first play will win or lose? Have you met this questi

[LintCode] Coins in a Line II

There are n coins with different value in a line. Two players take turns to take one or two coins from left side until there are no more coins left. The player who take the coins with the most value wins. Could you please decide the first player will

[LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈

Alex and Lee play a game with piles of stones.  There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i]. The objective of the game is to end with the most stones.  The total number of stones

Coins in a Line

ref: http://www.lintcode.com/en/problem/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

LeetCode 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 play will win or lose? 该题类似于下题: 箱子里面有一百个球,甲和乙分别

Coins in a Line I &amp; II

Coins in a Line I 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 play will win or lose? Exampl

Coins in a Line III

There are n coins in a line. 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. Could you please decide the first player will win or lose? Given

[LintCode] 395. Coins in a Line 2_Medium tag: Dynamic Programming, 博弈

Description There are n coins with different value in a line. Two players take turns to take one or two coins from left side until there are no more coins left. The player who take the coins with the most value wins. Could you please decide the first