Skip to content

Commit cdacd48

Browse files
authored
Merge pull request #1194 from 0xff-dev/822
Add solution and test-cases for problem 822
2 parents c7bdf30 + 37d49e4 commit cdacd48

File tree

3 files changed

+65
-27
lines changed

3 files changed

+65
-27
lines changed

leetcode/801-900/0822.Card-Flipping-Game/README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
# [822.Card Flipping Game][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given two **0-indexed** integer arrays `fronts` and `backs` of length `n`, where the `ith` card has the positive integer `fronts[i]` printed on the front and `backs[i]` printed on the back. Initially, each card is placed on a table such that the front number is facing up and the other is facing down. You may flip over any number of cards (possibly zero).
5+
6+
After flipping the cards, an integer is considered *8good** if it is facing down on some card and **not** facing up on any card.
7+
8+
Return the minimum possible good integer after flipping the cards. If there are no good integers, return `0`.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: fronts = [1,2,4,4,7], backs = [1,3,4,1,3]
14+
Output: 2
15+
Explanation:
16+
If we flip the second card, the face up numbers are [1,3,4,4,7] and the face down are [1,2,4,1,3].
17+
2 is the minimum good integer as it appears facing down but not facing up.
18+
It can be shown that 2 is the minimum possible good integer obtainable after flipping some cards.
1319
```
1420

15-
## 题意
16-
> ...
21+
**Example 2:**
1722

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Card Flipping Game
23-
```go
2423
```
25-
24+
Input: fronts = [1], backs = [1]
25+
Output: 0
26+
Explanation:
27+
There are no good integers no matter how we flip the cards, so we return 0.
28+
```
2629

2730
## 结语
2831

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(fronts []int, backs []int) int {
4+
fm := make(map[int]struct{})
5+
for _, n := range fronts {
6+
fm[n] = struct{}{}
7+
}
8+
ans := 0
9+
for _, n := range backs {
10+
if _, ok := fm[n]; !ok {
11+
if ans == 0 || ans > n {
12+
ans = n
13+
}
14+
}
15+
}
16+
mm := make(map[int][]int)
17+
keys := make([]int, 0)
18+
for i, f := range fronts {
19+
if _, ok := mm[f]; !ok {
20+
mm[f] = make([]int, 0)
21+
keys = append(keys, f)
22+
}
23+
mm[f] = append(mm[f], backs[i])
24+
}
25+
for i := 0; i < len(keys); i++ {
26+
ok := true
27+
for _, v := range mm[keys[i]] {
28+
if v == keys[i] {
29+
ok = false
30+
break
31+
}
32+
}
33+
if !ok {
34+
continue
35+
}
36+
if ans == 0 || ans > keys[i] {
37+
ans = keys[i]
38+
}
39+
}
40+
return ans
541
}

leetcode/801-900/0822.Card-Flipping-Game/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
fronts, backs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 2, 4, 4, 7}, []int{1, 3, 4, 1, 3}, 2},
17+
{"TestCase2", []int{1}, []int{1}, 0},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.fronts, c.backs)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.fronts, c.backs)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)