Nim Game Leetcode python
My first impression is to use dp, no need to store all three values just use a,b,c
a is if I can win, if I pick one nim, b is if I pick two nim, c is if I pick three nim
But this will cause Memory Error in python
Then I used a loop and printed all the results within range 10, and found only if it's the multiple of 4 that will return False
Runtime: 32 ms
class Solution(object):
def canWinNim(self, n):
"""
:type n: int
:rtype: bool
"""
# a,b,c = False,False,False
# for x in range(1,n+1):
# current = False
# if a == False:
# current = True
# if b == False:
# current = True
# if c == False:
# current = True
# a,b,c = current,a,b
# return a
return n%4 !=0
s = Solution()
for x in xrange(10):
print s.canWinNim(x)
评论