-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
68 lines (62 loc) · 2.18 KB
/
main.cpp
File metadata and controls
68 lines (62 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Source: https://leetcode.com/problems/alice-and-bob-playing-flower-game
// Title: Alice and Bob Playing Flower Game
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Alice and Bob are playing a turn-based game on a field, with two lanes of flowers between them. There are `x` flowers in the first lane between Alice and Bob, and `y` flowers in the second lane between them.
//
// https://assets.leetcode.com/uploads/2025/08/27/3021.png
//
// The game proceeds as follows:
//
// - Alice takes the first turn.
// - In each turn, a player must choose either one of the laneand pick one flower from that side.
// - At the end of the turn, if there are no flowers left at all, the **current** player captures their opponent and wins the game.
//
// Given two integers, `n` and `m`, the task is to compute the number of possible pairs `(x, y)` that satisfy the conditions:
//
// - Alice must win the game according to the described rules.
// - The number of flowers `x` in the first lane must be in the range `[1,n]`.
// - The number of flowers `y` in the second lane must be in the range `[1,m]`.
//
// Return the number of possible pairs `(x, y)` that satisfy the conditions mentioned in the statement.
//
// **Example 1:**
//
// ```
// Input: n = 3, m = 2
// Output: 3
// Explanation: The following pairs satisfy conditions described in the statement: (1,2), (3,2), (2,1).
// ```
//
// **Example 2:**
//
// ```
// Input: n = 1, m = 1
// Output: 0
// Explanation: No pairs satisfy the conditions described in the statement.
// ```
//
// **Constraints:**
//
// - `1 <= n, m <= 10^5`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using namespace std;
class Solution {
public:
long long flowerGame(int n, int m) {
long long evenX = n / 2;
long long oddX = (n + 1) / 2;
long long evenY = m / 2;
long long oddY = (m + 1) / 2;
return evenX * oddY + evenY * oddX;
}
};
class Solution2 {
public:
long long flowerGame(int n, int m) {
return (long long)m * n / 2;
;
}
};