C++ recursion solution


  • 0
    Y
    class Solution {
    public:
        bool canWinNim(int n) {
            if(n<=3)return true;
            for(int i=1;i<=3;i++){
                if(!canWinNim(n-i))return true;
            }
            return false;
        }
    };
    

    This solution can't be accepted tho, just an idea.


  • 0
    N

    [hint] I guess you can not pass it because time out. it's recurrences. f(n) = !(f(n-1) &f(n-2)&f(n-3)) if n > 3
    so expand the recurrence and see if you can find any rule there.


  • 0
    Y

    I tested it in my IDE and it works, but it can not pass because time out. Never mind it is just my first thought when I saw this question.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.