Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created August 29, 2025 22:56
Show Gist options
  • Save Ifihan/9c742200613cf7ad195b71e15b0dab5c to your computer and use it in GitHub Desktop.
Save Ifihan/9c742200613cf7ad195b71e15b0dab5c to your computer and use it in GitHub Desktop.
Alice and Bob Playing Flower Game

Question

Approach

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.

Implementation

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

Complexities

  • Time: O(1)
  • Space: O(1)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment