Original link: https://leetcode.com/problems/divisor-game/
The code is super stupid.
class Solution(object):
def divisorGame(self, N):
"""
:type N: int
:rtype: bool
"""
return N % 2 == 0
The point here is to mathmatically prove that we can do that here.
We have
- Odd = 1 * odd * odd * … * odd
- Even = 1 * … * even
So for people who has an odd, he have to return an even (Odd - odd = even)
For people who has an even, he have two choices.
We know that if Alice has 3, she will lose. But if she has 2 or 4, she will win.
So we can design a strategy which is for Alice, always try to gives odd. As the number is decreasing, the number given by Bob is always even and will finally be 4.
But if Aice gets an odd first, she has no choice but gives an even. Bob will take the same strategy to give an odd again and finally win.
So what we care is only whether the first number for Alice is Even.