Skip to content

Commit 45f96c0

Browse files
authored
feat: add solutions to lc problem: No.3021 (#2283)
No.3021.Alice and Bob Playing Flower Game
1 parent c5c070d commit 45f96c0

File tree

12 files changed

+240
-65
lines changed

12 files changed

+240
-65
lines changed

solution/3000-3099/3021.Alice and Bob Playing Flower Game/README.md

+97-21
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,107 @@
5454

5555
## 解法
5656

57-
### 方法一
57+
### 方法一:数学
58+
59+
根据题目描述,每一次行动,玩家都会选择顺时针或者逆时针方向,然后摘一朵鲜花。由于 Alice 先行动,因此当 $x + y$ 为奇数时,Alice 一定会赢得游戏。
60+
61+
因此,鲜花数目 $x$ 和 $y$ 满足以下条件:
62+
63+
1. $x + y$ 为奇数;
64+
2. $1 \le x \le n$;
65+
3. $1 \le y \le m$。
66+
67+
若 $x$ 为奇数,$y$ 一定为偶数。此时 $x$ 的取值个数为 $\lceil \frac{n}{2} \rceil$,$y$ 的取值个数为 $\lfloor \frac{m}{2} \rfloor$,因此满足条件的数对个数为 $\lceil \frac{n}{2} \rceil \times \lfloor \frac{m}{2} \rfloor$。
68+
69+
若 $x$ 为偶数,$y$ 一定为奇数。此时 $x$ 的取值个数为 $\lfloor \frac{n}{2} \rfloor$,$y$ 的取值个数为 $\lceil \frac{m}{2} \rceil$,因此满足条件的数对个数为 $\lfloor \frac{n}{2} \rfloor \times \lceil \frac{m}{2} \rceil$。
70+
71+
因此,满足条件的数对个数为 $\lceil \frac{n}{2} \rceil \times \lfloor \frac{m}{2} \rfloor + \lfloor \frac{n}{2} \rfloor \times \lceil \frac{m}{2} \rceil$,即 $\lfloor \frac{n + 1}{2} \rfloor \times \lfloor \frac{m}{2} \rfloor + \lfloor \frac{n}{2} \rfloor \times \lfloor \frac{m + 1}{2} \rfloor$。
72+
73+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
5874

5975
<!-- tabs:start -->
6076

6177
```python
6278
class Solution:
6379
def flowerGame(self, n: int, m: int) -> int:
64-
count = (n + 1) // 2
65-
tol = (m + 1) // 2
66-
ecount = n // 2
67-
etol = m // 2
68-
return count * etol + ecount * tol
80+
a1 = (n + 1) // 2
81+
b1 = (m + 1) // 2
82+
a2 = n // 2
83+
b2 = m // 2
84+
return a1 * b2 + a2 * b1
85+
```
6986

87+
```java
88+
class Solution {
89+
public long flowerGame(int n, int m) {
90+
long a1 = (n + 1) / 2;
91+
long b1 = (m + 1) / 2;
92+
long a2 = n / 2;
93+
long b2 = m / 2;
94+
return a1 * b2 + a2 * b1;
95+
}
96+
}
97+
```
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
long long flowerGame(int n, int m) {
103+
long long a1 = (n + 1) / 2;
104+
long long b1 = (m + 1) / 2;
105+
long long a2 = n / 2;
106+
long long b2 = m / 2;
107+
return a1 * b2 + a2 * b1;
108+
}
109+
};
110+
```
111+
112+
```go
113+
func flowerGame(n int, m int) int64 {
114+
a1, b1 := (n+1)/2, (m+1)/2
115+
a2, b2 := n/2, m/2
116+
return int64(a1*b2 + a2*b1)
117+
}
118+
```
119+
120+
```ts
121+
function flowerGame(n: number, m: number): number {
122+
const [a1, b1] = [(n + 1) >> 1, (m + 1) >> 1];
123+
const [a2, b2] = [n >> 1, m >> 1];
124+
return a1 * b2 + a2 * b1;
125+
}
126+
```
127+
128+
<!-- tabs:end -->
129+
130+
### 方法二:数学(优化)
131+
132+
方法一得出的结果为 $\lfloor \frac{n + 1}{2} \rfloor \times \lfloor \frac{m}{2} \rfloor + \lfloor \frac{n}{2} \rfloor \times \lfloor \frac{m + 1}{2} \rfloor$。
133+
134+
如果 $n$ 和 $m$ 都是奇数,那么结果为 $\frac{n + 1}{2} \times \frac{m - 1}{2} + \frac{n - 1}{2} \times \frac{m + 1}{2}$,即 $\frac{n \times m - 1}{2}$。
135+
136+
如果 $n$ 和 $m$ 都是偶数,那么结果为 $\frac{n}{2} \times \frac{m}{2} + \frac{n}{2} \times \frac{m}{2}$,即 $\frac{n \times m}{2}$。
137+
138+
如果 $n$ 是奇数,且 $m$ 是偶数,那么结果为 $\frac{n + 1}{2} \times \frac{m}{2} + \frac{n - 1}{2} \times \frac{m}{2}$,即 $\frac{n \times m}{2}$。
139+
140+
如果 $n$ 是偶数,且 $m$ 是奇数,那么结果为 $\frac{n}{2} \times \frac{m - 1}{2} + \frac{n}{2} \times \frac{m + 1}{2}$,即 $\frac{n \times m}{2}$。
141+
142+
上面四种情况可以合并为 $\lfloor \frac{n \times m}{2} \rfloor$。
143+
144+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
145+
146+
<!-- tabs:start -->
147+
148+
```python
149+
class Solution:
150+
def flowerGame(self, n: int, m: int) -> int:
151+
return (n * m) // 2
70152
```
71153

72154
```java
73155
class Solution {
74156
public long flowerGame(int n, int m) {
75-
long count = (n + 1) / 2;
76-
long tol = (m + 1) / 2;
77-
long ecount = n / 2;
78-
long etol = m / 2;
79-
return (count * etol + ecount * tol);
157+
return ((long) n * m) / 2;
80158
}
81159
}
82160
```
@@ -85,22 +163,20 @@ class Solution {
85163
class Solution {
86164
public:
87165
long long flowerGame(int n, int m) {
88-
long long count = (n + 1) / 2;
89-
long long tol = (m + 1) / 2;
90-
long long ecount = n / 2;
91-
long long etol = m / 2;
92-
return (count * etol + ecount * tol);
166+
return ((long long) n * m) / 2;
93167
}
94168
};
95169
```
96170
97171
```go
98172
func flowerGame(n int, m int) int64 {
99-
count := int64((n + 1) / 2)
100-
tol := int64((m + 1) / 2)
101-
ecount := int64(n / 2)
102-
etol := int64(m / 2)
103-
return count*etol + ecount*tol
173+
return int64((n * m) / 2)
174+
}
175+
```
176+
177+
```ts
178+
function flowerGame(n: number, m: number): number {
179+
return Number(((BigInt(n) * BigInt(m)) / 2n) | 0n);
104180
}
105181
```
106182

solution/3000-3099/3021.Alice and Bob Playing Flower Game/README_EN.md

+97-21
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,107 @@
5050

5151
## Solutions
5252

53-
### Solution 1
53+
### Solution 1: Mathematics
54+
55+
According to the problem description, in each move, the player will choose to move in a clockwise or counterclockwise direction and then pick a flower. Since Alice moves first, when $x + y$ is odd, Alice will definitely win the game.
56+
57+
Therefore, the number of flowers $x$ and $y$ meet the following conditions:
58+
59+
1. $x + y$ is odd;
60+
2. $1 \le x \le n$;
61+
3. $1 \le y \le m$.
62+
63+
If $x$ is odd, $y$ must be even. At this time, the number of values of $x$ is $\lceil \frac{n}{2} \rceil$, the number of values of $y$ is $\lfloor \frac{m}{2} \rfloor$, so the number of pairs that meet the conditions is $\lceil \frac{n}{2} \rceil \times \lfloor \frac{m}{2} \rfloor$.
64+
65+
If $x$ is even, $y$ must be odd. At this time, the number of values of $x$ is $\lfloor \frac{n}{2} \rfloor$, the number of values of $y$ is $\lceil \frac{m}{2} \rceil$, so the number of pairs that meet the conditions is $\lfloor \frac{n}{2} \rfloor \times \lceil \frac{m}{2} \rceil$.
66+
67+
Therefore, the number of pairs that meet the conditions is $\lceil \frac{n}{2} \rceil \times \lfloor \frac{m}{2} \rfloor + \lfloor \frac{n}{2} \rfloor \times \lceil \frac{m}{2} \rceil$, which is $\lfloor \frac{n + 1}{2} \rfloor \times \lfloor \frac{m}{2} \rfloor + \lfloor \frac{n}{2} \rfloor \times \lfloor \frac{m + 1}{2} \rfloor$.
68+
69+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
5470

5571
<!-- tabs:start -->
5672

5773
```python
5874
class Solution:
5975
def flowerGame(self, n: int, m: int) -> int:
60-
count = (n + 1) // 2
61-
tol = (m + 1) // 2
62-
ecount = n // 2
63-
etol = m // 2
64-
return count * etol + ecount * tol
76+
a1 = (n + 1) // 2
77+
b1 = (m + 1) // 2
78+
a2 = n // 2
79+
b2 = m // 2
80+
return a1 * b2 + a2 * b1
81+
```
6582

83+
```java
84+
class Solution {
85+
public long flowerGame(int n, int m) {
86+
long a1 = (n + 1) / 2;
87+
long b1 = (m + 1) / 2;
88+
long a2 = n / 2;
89+
long b2 = m / 2;
90+
return a1 * b2 + a2 * b1;
91+
}
92+
}
93+
```
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
long long flowerGame(int n, int m) {
99+
long long a1 = (n + 1) / 2;
100+
long long b1 = (m + 1) / 2;
101+
long long a2 = n / 2;
102+
long long b2 = m / 2;
103+
return a1 * b2 + a2 * b1;
104+
}
105+
};
106+
```
107+
108+
```go
109+
func flowerGame(n int, m int) int64 {
110+
a1, b1 := (n+1)/2, (m+1)/2
111+
a2, b2 := n/2, m/2
112+
return int64(a1*b2 + a2*b1)
113+
}
114+
```
115+
116+
```ts
117+
function flowerGame(n: number, m: number): number {
118+
const [a1, b1] = [(n + 1) >> 1, (m + 1) >> 1];
119+
const [a2, b2] = [n >> 1, m >> 1];
120+
return a1 * b2 + a2 * b1;
121+
}
122+
```
123+
124+
<!-- tabs:end -->
125+
126+
### Solution 2: Mathematics (Optimized)
127+
128+
The result obtained from Solution 1 is $\lfloor \frac{n + 1}{2} \rfloor \times \lfloor \frac{m}{2} \rfloor + \lfloor \frac{n}{2} \rfloor \times \lfloor \frac{m + 1}{2} \rfloor$.
129+
130+
If both $n$ and $m$ are odd, then the result is $\frac{n + 1}{2} \times \frac{m - 1}{2} + \frac{n - 1}{2} \times \frac{m + 1}{2}$, which is $\frac{n \times m - 1}{2}$.
131+
132+
If both $n$ and $m$ are even, then the result is $\frac{n}{2} \times \frac{m}{2} + \frac{n}{2} \times \frac{m}{2}$, which is $\frac{n \times m}{2}$.
133+
134+
If $n$ is odd and $m$ is even, then the result is $\frac{n + 1}{2} \times \frac{m}{2} + \frac{n - 1}{2} \times \frac{m}{2}$, which is $\frac{n \times m}{2}$.
135+
136+
If $n$ is even and $m$ is odd, then the result is $\frac{n}{2} \times \frac{m - 1}{2} + \frac{n}{2} \times \frac{m + 1}{2}$, which is $\frac{n \times m}{2}$.
137+
138+
The above four cases can be combined into $\lfloor \frac{n \times m}{2} \rfloor$.
139+
140+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
141+
142+
<!-- tabs:start -->
143+
144+
```python
145+
class Solution:
146+
def flowerGame(self, n: int, m: int) -> int:
147+
return (n * m) // 2
66148
```
67149

68150
```java
69151
class Solution {
70152
public long flowerGame(int n, int m) {
71-
long count = (n + 1) / 2;
72-
long tol = (m + 1) / 2;
73-
long ecount = n / 2;
74-
long etol = m / 2;
75-
return (count * etol + ecount * tol);
153+
return ((long) n * m) / 2;
76154
}
77155
}
78156
```
@@ -81,22 +159,20 @@ class Solution {
81159
class Solution {
82160
public:
83161
long long flowerGame(int n, int m) {
84-
long long count = (n + 1) / 2;
85-
long long tol = (m + 1) / 2;
86-
long long ecount = n / 2;
87-
long long etol = m / 2;
88-
return (count * etol + ecount * tol);
162+
return ((long long) n * m) / 2;
89163
}
90164
};
91165
```
92166
93167
```go
94168
func flowerGame(n int, m int) int64 {
95-
count := int64((n + 1) / 2)
96-
tol := int64((m + 1) / 2)
97-
ecount := int64(n / 2)
98-
etol := int64(m / 2)
99-
return count*etol + ecount*tol
169+
return int64((n * m) / 2)
170+
}
171+
```
172+
173+
```ts
174+
function flowerGame(n: number, m: number): number {
175+
return Number(((BigInt(n) * BigInt(m)) / 2n) | 0n);
100176
}
101177
```
102178

Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Solution {
22
public:
33
long long flowerGame(int n, int m) {
4-
long long count = (n + 1) / 2;
5-
long long tol = (m + 1) / 2;
6-
long long ecount = n / 2;
7-
long long etol = m / 2;
8-
return (count * etol + ecount * tol);
4+
long long a1 = (n + 1) / 2;
5+
long long b1 = (m + 1) / 2;
6+
long long a2 = n / 2;
7+
long long b2 = m / 2;
8+
return a1 * b2 + a2 * b1;
99
}
10-
};
10+
};
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
func flowerGame(n int, m int) int64 {
2-
count := int64((n + 1) / 2)
3-
tol := int64((m + 1) / 2)
4-
ecount := int64(n / 2)
5-
etol := int64(m / 2)
6-
return count*etol + ecount*tol
7-
}
2+
a1, b1 := (n+1)/2, (m+1)/2
3+
a2, b2 := n/2, m/2
4+
return int64(a1*b2 + a2*b1)
5+
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Solution {
22
public long flowerGame(int n, int m) {
3-
long count = (n + 1) / 2;
4-
long tol = (m + 1) / 2;
5-
long ecount = n / 2;
6-
long etol = m / 2;
7-
return (count * etol + ecount * tol);
3+
long a1 = (n + 1) / 2;
4+
long b1 = (m + 1) / 2;
5+
long a2 = n / 2;
6+
long b2 = m / 2;
7+
return a1 * b2 + a2 * b1;
88
}
9-
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def flowerGame(self, n: int, m: int) -> int:
3-
count = (n + 1) // 2
4-
tol = (m + 1) // 2
5-
ecount = n // 2
6-
etol = m // 2
7-
return count * etol + ecount * tol
3+
a1 = (n + 1) // 2
4+
b1 = (m + 1) // 2
5+
a2 = n // 2
6+
b2 = m // 2
7+
return a1 * b2 + a2 * b1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function flowerGame(n: number, m: number): number {
2+
const [a1, b1] = [(n + 1) >> 1, (m + 1) >> 1];
3+
const [a2, b2] = [n >> 1, m >> 1];
4+
return a1 * b2 + a2 * b1;
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
long long flowerGame(int n, int m) {
4+
return ((long long) n * m) / 2;
5+
}
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func flowerGame(n int, m int) int64 {
2+
return int64((n * m) / 2)
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
public long flowerGame(int n, int m) {
3+
return ((long) n * m) / 2;
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def flowerGame(self, n: int, m: int) -> int:
3+
return (n * m) // 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function flowerGame(n: number, m: number): number {
2+
return Number(((BigInt(n) * BigInt(m)) / 2n) | 0n);
3+
}

0 commit comments

Comments
 (0)