• 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);
        }
    };
  • 相关阅读:
    array_unshift() 、
    readfile() 函数
    Java的异常处理
    Java 接口
    Java 抽象类
    final关键字
    statice关键字
    dom查询
    JS 正则表达式
    JS对象
  • 原文地址:https://www.cnblogs.com/tonix/p/4853264.html
Copyright © 2020-2023  润新知