码迷,mamicode.com
首页 > 其他好文 > 详细

LintCode "Coins in a Line"

时间:2015-10-03 13:12:54      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

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);
    }
};

LintCode "Coins in a Line"

标签:

原文地址:http://www.cnblogs.com/tonix/p/4853264.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!