I noticed each move removes exactly one flower from either lane, so the total number of moves is x + y. Since I move first, I win iff the total number of moves is odd (I take moves 1,3,5,…,x+y). Therefore, I just need to count pairs (x, y) with opposite parity. Let oddN = (n+1)//2, evenN = n//2, and similarly for m. The answer is oddN * evenM + evenN * oddM.
class Solution:
def flowerGame(self, n: int, m: int) -> int:
oddN = (n + 1) // 2
evenN = n // 2
oddM = (m + 1) // 2
evenM = m // 2
return oddN * evenM + evenN * oddM
- Time: O(1)
- Space: O(1)
