🧠 Solving LeetCode Until I Become Top 1% — Day `70`

🔹 Problem: 3021. Alice and Bob Playing Flower Game

Difficulty: #Medium
Tags: #Math, #Combinatorics

📝 Problem Summary

Alice and Bob play a game with n flowers and m flowers. Each chooses one flower. Alice wins if the total numb…


This content originally appeared on DEV Community and was authored by Md. Rishat Talukder

🔹 Problem: 3021. Alice and Bob Playing Flower Game

Difficulty: #Medium
Tags: #Math, #Combinatorics

📝 Problem Summary

Alice and Bob play a game with n flowers and m flowers. Each chooses one flower. Alice wins if the total number of petals is odd.

You need to count the number of winning pairs (x, y) where 1 ≤ x ≤ n and 1 ≤ y ≤ m.

🧠 My Thought Process

  • Brute Force Idea:
    Loop through all pairs (x, y) and check if x + y is odd. That would be O(n * m), which is too slow for large inputs.

  • Optimized Strategy:

    • The solution was unexpectedly simple once I realized the parity logic:
    • Alice wins only when one number is even and the other is odd.
    • Count how many odds and evens are in 1..n and 1..m.
    • odds_in_n = (n + 1) // 2
    • evens_in_n = n // 2
    • odds_in_m = (m + 1) // 2
    • evens_in_m = m // 2
    • Winning pairs = (odds_in_n * evens_in_m) + (evens_in_n * odds_in_m)
    • Simplifies to (n * m) // 2.

⚙️ Code Implementation (Python)

class Solution:
    def flowerGame(self, n: int, m: int) -> int:
        return (n * m) // 2

⏱️ Time & Space Complexity

  • Time: O(1)
  • Space: O(1)

🧩 Key Takeaways

  • ✅ Learned how to reduce brute-force counting to simple parity logic.
  • 💡 The trick is noticing that half of all pairs will have odd sums.
  • 💭 In future, whenever I see problems about sums being odd/even, I’ll try counting odds and evens separately instead of brute force.

🔁 Reflection (Self-Check)

  • [x] Could I solve this without help?
  • [x] Did I write code from scratch?
  • [x] Did I understand why it works?
  • [x] Will I be able to recall this in a week? → Let’s see 😅

🚀 Progress Tracker

Metric Value
Day 70
Total Problems Solved 431
Confidence Today 😃
Leetcode Rating 1530


This content originally appeared on DEV Community and was authored by Md. Rishat Talukder


Print Share Comment Cite Upload Translate Updates
APA

Md. Rishat Talukder | Sciencx (2025-08-29T10:52:39+00:00) 🧠 Solving LeetCode Until I Become Top 1% — Day `70`. Retrieved from https://www.scien.cx/2025/08/29/%f0%9f%a7%a0-solving-leetcode-until-i-become-top-1-day-70/

MLA
" » 🧠 Solving LeetCode Until I Become Top 1% — Day `70`." Md. Rishat Talukder | Sciencx - Friday August 29, 2025, https://www.scien.cx/2025/08/29/%f0%9f%a7%a0-solving-leetcode-until-i-become-top-1-day-70/
HARVARD
Md. Rishat Talukder | Sciencx Friday August 29, 2025 » 🧠 Solving LeetCode Until I Become Top 1% — Day `70`., viewed ,<https://www.scien.cx/2025/08/29/%f0%9f%a7%a0-solving-leetcode-until-i-become-top-1-day-70/>
VANCOUVER
Md. Rishat Talukder | Sciencx - » 🧠 Solving LeetCode Until I Become Top 1% — Day `70`. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/08/29/%f0%9f%a7%a0-solving-leetcode-until-i-become-top-1-day-70/
CHICAGO
" » 🧠 Solving LeetCode Until I Become Top 1% — Day `70`." Md. Rishat Talukder | Sciencx - Accessed . https://www.scien.cx/2025/08/29/%f0%9f%a7%a0-solving-leetcode-until-i-become-top-1-day-70/
IEEE
" » 🧠 Solving LeetCode Until I Become Top 1% — Day `70`." Md. Rishat Talukder | Sciencx [Online]. Available: https://www.scien.cx/2025/08/29/%f0%9f%a7%a0-solving-leetcode-until-i-become-top-1-day-70/. [Accessed: ]
rf:citation
» 🧠 Solving LeetCode Until I Become Top 1% — Day `70` | Md. Rishat Talukder | Sciencx | https://www.scien.cx/2025/08/29/%f0%9f%a7%a0-solving-leetcode-until-i-become-top-1-day-70/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.