Google | Phone Screen | New Grad
Anonymous User
1062

Anyone know how to do this? Looks like a DP approach but couldnt seem to figure it out
You start at a given number, and there are two players. Each player can subtract [1, 2, or 5]. The person who reaches 0 loses. Calculate which player would win given player 1 moves first.

I'm looking to a simialr solution like this. Maybe memo + make it generic input

class Solution {
    public boolean canWinNim(int n) {
        if(n <= 0) return false;
        if(n == 1 || n == 2 || n== 5) return true;
        if(canWinNim(n-1) && canWinNim(n-2) && canWinNim(n-5)) return false;
        return true;
    }
}
Comments (7)