1 liner with explanations


  • 30
    P
    class Solution(object):
        def canWinNim(self, n):
            """
            :type n: int
            :rtype: bool
            """
            # strategy: the one with 4 remaining must loose
            # A, B players
            # if n == 4k, then at each round B can make A+B both take 4, 
            # eventually leave 4 to A, A lose
            # if n == 4k + i (i <= 3), then A can always take i first and B will
            # finanly lose as he faces above scenario like A
    
            return bool(n%4!=0)

  • 3
    D

    I think you don't need to wrap it in bool method. just n% 4 != 0 should do just fine.


Log in to reply
 

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