|
| 1 | +# [3021. Alice and Bob Playing Flower Game](https://leetcode.com/problems/alice-and-bob-playing-flower-game) |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +<div class="elfjS" data-track-load="description_content"><p>Alice and Bob are playing a turn-based game on a field, with two lanes of flowers between them. There are <code>x</code> flowers in the first lane between Alice and Bob, and <code>y</code> flowers in the second lane between them.</p> |
| 6 | + |
| 7 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2025/08/27/3021.png" style="width: 300px; height: 150px;"></p> |
| 8 | + |
| 9 | +<p>The game proceeds as follows:</p> |
| 10 | + |
| 11 | +<ol> |
| 12 | + <li>Alice takes the first turn.</li> |
| 13 | + <li>In each turn, a player must choose either one of the lane and pick one flower from that side.</li> |
| 14 | + <li>At the end of the turn, if there are no flowers left at all, the <strong>current</strong> player captures their opponent and wins the game.</li> |
| 15 | +</ol> |
| 16 | + |
| 17 | +<p>Given two integers, <code>n</code> and <code>m</code>, the task is to compute the number of possible pairs <code>(x, y)</code> that satisfy the conditions:</p> |
| 18 | + |
| 19 | +<ul> |
| 20 | + <li>Alice must win the game according to the described rules.</li> |
| 21 | + <li>The number of flowers <code>x</code> in the first lane must be in the range <code>[1,n]</code>.</li> |
| 22 | + <li>The number of flowers <code>y</code> in the second lane must be in the range <code>[1,m]</code>.</li> |
| 23 | +</ul> |
| 24 | + |
| 25 | +<p>Return <em>the number of possible pairs</em> <code>(x, y)</code> <em>that satisfy the conditions mentioned in the statement</em>.</p> |
| 26 | + |
| 27 | +<p> </p> |
| 28 | +<p><strong class="example">Example 1:</strong></p> |
| 29 | + |
| 30 | +<pre><strong>Input:</strong> n = 3, m = 2 |
| 31 | +<strong>Output:</strong> 3 |
| 32 | +<strong>Explanation:</strong> The following pairs satisfy conditions described in the statement: (1,2), (3,2), (2,1). |
| 33 | +</pre> |
| 34 | + |
| 35 | +<p><strong class="example">Example 2:</strong></p> |
| 36 | + |
| 37 | +<pre><strong>Input:</strong> n = 1, m = 1 |
| 38 | +<strong>Output:</strong> 0 |
| 39 | +<strong>Explanation:</strong> No pairs satisfy the conditions described in the statement. |
| 40 | +</pre> |
| 41 | + |
| 42 | +<p> </p> |
| 43 | +<p><strong>Constraints:</strong></p> |
| 44 | + |
| 45 | +<ul> |
| 46 | + <li><code>1 <= n, m <= 10<sup>5</sup></code></li> |
| 47 | +</ul> |
| 48 | +</div> |
| 49 | + |
| 50 | +<p> </p> |
| 51 | + |
| 52 | +## Solutions |
| 53 | + |
| 54 | +**Solution: `Math`** |
| 55 | + |
| 56 | +- Time complexity: <em>O(1)</em> |
| 57 | +- Space complexity: <em>O(1)</em> |
| 58 | + |
| 59 | +<p> </p> |
| 60 | + |
| 61 | +### **JavaScript** |
| 62 | + |
| 63 | +```js |
| 64 | +/** |
| 65 | + * @param {number} n |
| 66 | + * @param {number} m |
| 67 | + * @return {number} |
| 68 | + */ |
| 69 | +const flowerGame = function (n, m) { |
| 70 | + const countEvenN = Math.floor(n / 2); |
| 71 | + const countOddN = n - countEvenN; |
| 72 | + const countEvenM = Math.floor(m / 2); |
| 73 | + const countOddM = m - countEvenM; |
| 74 | + |
| 75 | + return countOddN * countEvenM + countEvenN * countOddM; |
| 76 | +}; |
| 77 | +``` |
0 commit comments