From 37d49e4099336533b7e96aaac647c377064a24be Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sat, 3 May 2025 14:13:08 +0800 Subject: [PATCH] Add solution and test-cases for problem 822 --- .../801-900/0822.Card-Flipping-Game/README.md | 31 +++++++------- .../0822.Card-Flipping-Game/Solution.go | 40 ++++++++++++++++++- .../0822.Card-Flipping-Game/Solution_test.go | 21 +++++----- 3 files changed, 65 insertions(+), 27 deletions(-) diff --git a/leetcode/801-900/0822.Card-Flipping-Game/README.md b/leetcode/801-900/0822.Card-Flipping-Game/README.md index 2226d2943..59a742fc7 100644 --- a/leetcode/801-900/0822.Card-Flipping-Game/README.md +++ b/leetcode/801-900/0822.Card-Flipping-Game/README.md @@ -1,28 +1,31 @@ # [822.Card Flipping Game][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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). + +After flipping the cards, an integer is considered *8good** if it is facing down on some card and **not** facing up on any card. + +Return the minimum possible good integer after flipping the cards. If there are no good integers, return `0`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: fronts = [1,2,4,4,7], backs = [1,3,4,1,3] +Output: 2 +Explanation: +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]. +2 is the minimum good integer as it appears facing down but not facing up. +It can be shown that 2 is the minimum possible good integer obtainable after flipping some cards. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Card Flipping Game -```go ``` - +Input: fronts = [1], backs = [1] +Output: 0 +Explanation: +There are no good integers no matter how we flip the cards, so we return 0. +``` ## 结语 diff --git a/leetcode/801-900/0822.Card-Flipping-Game/Solution.go b/leetcode/801-900/0822.Card-Flipping-Game/Solution.go index d115ccf5e..a049b80f7 100644 --- a/leetcode/801-900/0822.Card-Flipping-Game/Solution.go +++ b/leetcode/801-900/0822.Card-Flipping-Game/Solution.go @@ -1,5 +1,41 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(fronts []int, backs []int) int { + fm := make(map[int]struct{}) + for _, n := range fronts { + fm[n] = struct{}{} + } + ans := 0 + for _, n := range backs { + if _, ok := fm[n]; !ok { + if ans == 0 || ans > n { + ans = n + } + } + } + mm := make(map[int][]int) + keys := make([]int, 0) + for i, f := range fronts { + if _, ok := mm[f]; !ok { + mm[f] = make([]int, 0) + keys = append(keys, f) + } + mm[f] = append(mm[f], backs[i]) + } + for i := 0; i < len(keys); i++ { + ok := true + for _, v := range mm[keys[i]] { + if v == keys[i] { + ok = false + break + } + } + if !ok { + continue + } + if ans == 0 || ans > keys[i] { + ans = keys[i] + } + } + return ans } diff --git a/leetcode/801-900/0822.Card-Flipping-Game/Solution_test.go b/leetcode/801-900/0822.Card-Flipping-Game/Solution_test.go index 14ff50eb4..7de394c11 100644 --- a/leetcode/801-900/0822.Card-Flipping-Game/Solution_test.go +++ b/leetcode/801-900/0822.Card-Flipping-Game/Solution_test.go @@ -9,31 +9,30 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + fronts, backs []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 2, 4, 4, 7}, []int{1, 3, 4, 1, 3}, 2}, + {"TestCase2", []int{1}, []int{1}, 0}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.fronts, c.backs) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.fronts, c.backs) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }