diff --git a/solution/0800-0899/0822.Card Flipping Game/README.md b/solution/0800-0899/0822.Card Flipping Game/README.md index 916b116f05012..460459ddaa715 100644 --- a/solution/0800-0899/0822.Card Flipping Game/README.md +++ b/solution/0800-0899/0822.Card Flipping Game/README.md @@ -66,7 +66,7 @@ tags: ### 方法一:哈希表 -我们注意到,对于位置 $i$,若 $fronts[i]$ 与 $backs[i]$ 元素相同,则一定不满足条件。 +我们注意到,对于位置 $i$,若 $\textit{fronts}[i]$ 与 $\textit{backs}[i]$ 元素相同,则一定不满足条件。 因此,我们先找出正面与背面相同的元素,记录在哈希表 $s$ 中。 @@ -195,6 +195,43 @@ function flipgame(fronts: number[], backs: number[]): number { } ``` +#### Rust + +```rust +use std::collections::HashSet; + +impl Solution { + pub fn flipgame(fronts: Vec, backs: Vec) -> i32 { + let n = fronts.len(); + let mut s: HashSet = HashSet::new(); + + for i in 0..n { + if fronts[i] == backs[i] { + s.insert(fronts[i]); + } + } + + let mut ans = 9999; + for &v in fronts.iter() { + if !s.contains(&v) { + ans = ans.min(v); + } + } + for &v in backs.iter() { + if !s.contains(&v) { + ans = ans.min(v); + } + } + + if ans == 9999 { + 0 + } else { + ans + } + } +} +``` + #### C# ```cs diff --git a/solution/0800-0899/0822.Card Flipping Game/README_EN.md b/solution/0800-0899/0822.Card Flipping Game/README_EN.md index 89abfd94a2f80..dca0480e715a6 100644 --- a/solution/0800-0899/0822.Card Flipping Game/README_EN.md +++ b/solution/0800-0899/0822.Card Flipping Game/README_EN.md @@ -59,7 +59,17 @@ There are no good integers no matter how we flip the cards, so we return 0. -### Solution 1 +### Solution 1: Hash Table + +We observe that for position $i$, if $\textit{fronts}[i]$ is equal to $\textit{backs}[i]$, then it certainly does not satisfy the condition. + +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$. + +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. + +Finally, if we find any element that satisfies the condition, we return the minimum answer; otherwise, we return $0$. + +The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the arrays. @@ -180,6 +190,43 @@ function flipgame(fronts: number[], backs: number[]): number { } ``` +#### Rust + +```rust +use std::collections::HashSet; + +impl Solution { + pub fn flipgame(fronts: Vec, backs: Vec) -> i32 { + let n = fronts.len(); + let mut s: HashSet = HashSet::new(); + + for i in 0..n { + if fronts[i] == backs[i] { + s.insert(fronts[i]); + } + } + + let mut ans = 9999; + for &v in fronts.iter() { + if !s.contains(&v) { + ans = ans.min(v); + } + } + for &v in backs.iter() { + if !s.contains(&v) { + ans = ans.min(v); + } + } + + if ans == 9999 { + 0 + } else { + ans + } + } +} +``` + #### C# ```cs diff --git a/solution/0800-0899/0822.Card Flipping Game/Solution.rs b/solution/0800-0899/0822.Card Flipping Game/Solution.rs new file mode 100644 index 0000000000000..25f15912f0308 --- /dev/null +++ b/solution/0800-0899/0822.Card Flipping Game/Solution.rs @@ -0,0 +1,32 @@ +use std::collections::HashSet; + +impl Solution { + pub fn flipgame(fronts: Vec, backs: Vec) -> i32 { + let n = fronts.len(); + let mut s: HashSet = HashSet::new(); + + for i in 0..n { + if fronts[i] == backs[i] { + s.insert(fronts[i]); + } + } + + let mut ans = 9999; + for &v in fronts.iter() { + if !s.contains(&v) { + ans = ans.min(v); + } + } + for &v in backs.iter() { + if !s.contains(&v) { + ans = ans.min(v); + } + } + + if ans == 9999 { + 0 + } else { + ans + } + } +}