Skip to content

Commit 6fe46bc

Browse files
authored
feat: add solutions to lc problem: No.0822 (#4301)
No.0822.Card Flipping Game
1 parent 7928aed commit 6fe46bc

File tree

3 files changed

+118
-2
lines changed

3 files changed

+118
-2
lines changed

solution/0800-0899/0822.Card Flipping Game/README.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ tags:
6666

6767
### 方法一:哈希表
6868

69-
我们注意到,对于位置 $i$,若 $fronts[i]$ 与 $backs[i]$ 元素相同,则一定不满足条件。
69+
我们注意到,对于位置 $i$,若 $\textit{fronts}[i]$ 与 $\textit{backs}[i]$ 元素相同,则一定不满足条件。
7070

7171
因此,我们先找出正面与背面相同的元素,记录在哈希表 $s$ 中。
7272

@@ -195,6 +195,43 @@ function flipgame(fronts: number[], backs: number[]): number {
195195
}
196196
```
197197

198+
#### Rust
199+
200+
```rust
201+
use std::collections::HashSet;
202+
203+
impl Solution {
204+
pub fn flipgame(fronts: Vec<i32>, backs: Vec<i32>) -> i32 {
205+
let n = fronts.len();
206+
let mut s: HashSet<i32> = HashSet::new();
207+
208+
for i in 0..n {
209+
if fronts[i] == backs[i] {
210+
s.insert(fronts[i]);
211+
}
212+
}
213+
214+
let mut ans = 9999;
215+
for &v in fronts.iter() {
216+
if !s.contains(&v) {
217+
ans = ans.min(v);
218+
}
219+
}
220+
for &v in backs.iter() {
221+
if !s.contains(&v) {
222+
ans = ans.min(v);
223+
}
224+
}
225+
226+
if ans == 9999 {
227+
0
228+
} else {
229+
ans
230+
}
231+
}
232+
}
233+
```
234+
198235
#### C#
199236

200237
```cs

solution/0800-0899/0822.Card Flipping Game/README_EN.md

+48-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,17 @@ There are no good integers no matter how we flip the cards, so we return 0.
5959

6060
<!-- solution:start -->
6161

62-
### Solution 1
62+
### Solution 1: Hash Table
63+
64+
We observe that for position $i$, if $\textit{fronts}[i]$ is equal to $\textit{backs}[i]$, then it certainly does not satisfy the condition.
65+
66+
Therefore, we first identify all elements that appear the same on both the front and back sides and record them in a hash set $s$.
67+
68+
Next, we iterate through all elements in both the front and back arrays. For any element $x$ that is **not** in the hash set $s$, we update the minimum value of the answer.
69+
70+
Finally, if we find any element that satisfies the condition, we return the minimum answer; otherwise, we return $0$.
71+
72+
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the arrays.
6373

6474
<!-- tabs:start -->
6575

@@ -180,6 +190,43 @@ function flipgame(fronts: number[], backs: number[]): number {
180190
}
181191
```
182192

193+
#### Rust
194+
195+
```rust
196+
use std::collections::HashSet;
197+
198+
impl Solution {
199+
pub fn flipgame(fronts: Vec<i32>, backs: Vec<i32>) -> i32 {
200+
let n = fronts.len();
201+
let mut s: HashSet<i32> = HashSet::new();
202+
203+
for i in 0..n {
204+
if fronts[i] == backs[i] {
205+
s.insert(fronts[i]);
206+
}
207+
}
208+
209+
let mut ans = 9999;
210+
for &v in fronts.iter() {
211+
if !s.contains(&v) {
212+
ans = ans.min(v);
213+
}
214+
}
215+
for &v in backs.iter() {
216+
if !s.contains(&v) {
217+
ans = ans.min(v);
218+
}
219+
}
220+
221+
if ans == 9999 {
222+
0
223+
} else {
224+
ans
225+
}
226+
}
227+
}
228+
```
229+
183230
#### C#
184231

185232
```cs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use std::collections::HashSet;
2+
3+
impl Solution {
4+
pub fn flipgame(fronts: Vec<i32>, backs: Vec<i32>) -> i32 {
5+
let n = fronts.len();
6+
let mut s: HashSet<i32> = HashSet::new();
7+
8+
for i in 0..n {
9+
if fronts[i] == backs[i] {
10+
s.insert(fronts[i]);
11+
}
12+
}
13+
14+
let mut ans = 9999;
15+
for &v in fronts.iter() {
16+
if !s.contains(&v) {
17+
ans = ans.min(v);
18+
}
19+
}
20+
for &v in backs.iter() {
21+
if !s.contains(&v) {
22+
ans = ans.min(v);
23+
}
24+
}
25+
26+
if ans == 9999 {
27+
0
28+
} else {
29+
ans
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)