From 155fc2cc8e6f29690a73388bd1ce92e107e0501d Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sun, 18 May 2025 16:38:14 +0800 Subject: [PATCH] Add solution and test-cases for problem 1931 --- .../1.png | Bin 0 -> 2266 bytes .../2.png | Bin 0 -> 2795 bytes .../README.md | 34 ++++++---- .../Solution.go | 64 +++++++++++++++++- .../Solution_test.go | 20 +++--- 5 files changed, 93 insertions(+), 25 deletions(-) create mode 100644 leetcode/1901-2000/1931.Painting-a-Grid-With-Three-Different-Colors/1.png create mode 100644 leetcode/1901-2000/1931.Painting-a-Grid-With-Three-Different-Colors/2.png diff --git a/leetcode/1901-2000/1931.Painting-a-Grid-With-Three-Different-Colors/1.png b/leetcode/1901-2000/1931.Painting-a-Grid-With-Three-Different-Colors/1.png new file mode 100644 index 0000000000000000000000000000000000000000..efacd3eb6da81f734b77abbb250b44f092acc57e GIT binary patch literal 2266 zcmeHJ-D@0G6yMlTlhE22M5@q|+N_O8migM*U2U{8JF_#B{g}zlW;e4+%uf3#~qu#wA;9Cf+&U3KVj$84xZL2Ot zVD*p}^?6Vnz=L5n5skQ!An*&ulmdQ?lAW@)rv&w~WdbA=fIW~0!+eMbhA|KjWmPU4 z|4l>Zv|XMb$2?xZ+CHJ;$Xfe!XtZeu#3=-c+qRXJ{7}OJ%WrdPO1WZK#WoGbJU-7( zz-gA0HmX^cRsb78+$aqM-cb_FHFmWVlTA$0Fh&_4N6RLhqj0*Ny(ohd+`j_>$TtMH zleE$aA%jJ=5C?82#8`nCNk!!GYK_Sibb&2NijlxWWHdR-S5Y?t6eS%NFOS$}Gc7~S zR$T&!kZ&lYzZMTROVvWQ$oNt)pEkHgacaDxC>*3GRbBOuu&hG>HYt=ur2ys4I&G_1 zP^VZ?^Lur{TeHfEq!L4U3CY+@D+;($E{Y?5#0W-wYH-%T%3|@OYgqS{XY{M9h(3Tw@RNOtxw+XSM`V3>7T2Q-dyPa@Y`tb@x+4z#P26(%3NK8@H;ap-W7Nmwqw@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rU6M} zqVL;{|ILy#_cs1{?AopJbXfB2z++Xla3fhgF*kec)u(=ZdU|o6@Sd-BV*I9fTp+y1 zI4F8~`aMy{+u!YO+<1IlDC*cFl=N4=?0KaA!k}p3*tvVVA90GX`ajC}#D^83Nq6H{%d&3`{g)d3q8&%PA=;p*~ChX$4OSLg8U%X1g8`g32O zmg99{>gz{mM9bPQ`BBN#;zp}+KkOQKtPh#HcBpw|>Go`2@0IG-%z|F;gS z@3^&Z|7Yj6dw)~g(tUrYwtpwiS1)#LyenD;t?{ zed-B+ZEZjI{z)k^cT(DWrN40>Fl)f;on+0$f8?%W`{uHP^49p&y3_fs5 z^ybo`w}RsNSI+!(7Pz0C44(R(=;`B9f0wNBWK?Wwbm5JjseR>-#$}D0U_>M0!XRFM zh^&pA>p5E6J6V$=1J@5V$=Vti=jVGaPfP~YAhP4q-v8r-p9g$-eD(VN)o0GmbkBj? h-~r)=tg<|}xnt_Vhi}|+F*@)QE3g_J*gI&j_Aka~GCBYN literal 0 HcmV?d00001 diff --git a/leetcode/1901-2000/1931.Painting-a-Grid-With-Three-Different-Colors/README.md b/leetcode/1901-2000/1931.Painting-a-Grid-With-Three-Different-Colors/README.md index eed26876d..d2d9778fb 100755 --- a/leetcode/1901-2000/1931.Painting-a-Grid-With-Three-Different-Colors/README.md +++ b/leetcode/1901-2000/1931.Painting-a-Grid-With-Three-Different-Colors/README.md @@ -1,28 +1,36 @@ # [1931.Painting a Grid With Three Different Colors][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 integers `m` and `n`. Consider an m x n grid where each cell is initially white. You can paint each cell **red**, **green**, or **blue**. All cells **must** be painted. + +Return the number of ways to color the grid with **no two adjacent cells having the same color**. Since the answer can be very large, return it **modulo** `10^9 + 7`. -**Example 1:** +**Example 1:** + +![1](./1.png) ``` -Input: a = "11", b = "1" -Output: "100" +Input: m = 1, n = 1 +Output: 3 +Explanation: The three possible colorings are shown in the image above. ``` -## 题意 -> ... +**Example 2:** -## 题解 +![2](./2.png) -### 思路1 -> ... -Painting a Grid With Three Different Colors -```go +``` +Input: m = 1, n = 2 +Output: 6 +Explanation: The six possible colorings are shown in the image above. ``` +**Example 3:** + +``` +Input: m = 5, n = 5 +Output: 580986 +``` ## 结语 diff --git a/leetcode/1901-2000/1931.Painting-a-Grid-With-Three-Different-Colors/Solution.go b/leetcode/1901-2000/1931.Painting-a-Grid-With-Three-Different-Colors/Solution.go index d115ccf5e..652749f92 100644 --- a/leetcode/1901-2000/1931.Painting-a-Grid-With-Three-Different-Colors/Solution.go +++ b/leetcode/1901-2000/1931.Painting-a-Grid-With-Three-Different-Colors/Solution.go @@ -1,5 +1,65 @@ package Solution -func Solution(x bool) bool { - return x +const mod1931 = 1e9 + 7 + +var colors = []byte{'R', 'G', 'B'} + +func columns(m int, cur string, preColor byte) []string { + ans := []string{} + if m == 0 { + ans = append(ans, cur) + return ans + } + + for _, color := range colors { + if color != preColor { + ans = append(ans, columns(m-1, cur+string(color), color)...) + } + } + return ans +} +func can(a, b string) bool { + for i := 0; i < len(a); i++ { + if a[i] == b[i] { + return false + } + } + return true +} + +func Solution(m int, n int) int { + groups := columns(m, "", ' ') + cache := make(map[string]map[int]int) + var dfs func(int, string) int + dfs = func(left int, cur string) int { + if left == 0 { + return 1 + } + if v, ok := cache[cur]; ok { + if c, ok1 := v[left]; ok1 { + return c + } + } + ans := 0 + for i := 0; i < len(groups); i++ { + if !can(cur, groups[i]) { + continue + } + ans = (ans + dfs(left-1, groups[i])) % mod1931 + } + + v, ok := cache[cur] + if !ok { + v = map[int]int{} + } + v[left] = ans + cache[cur] = v + return ans + } + // 挑选n个 + cur := "" + for i := 0; i < m; i++ { + cur += " " + } + return dfs(n, cur) } diff --git a/leetcode/1901-2000/1931.Painting-a-Grid-With-Three-Different-Colors/Solution_test.go b/leetcode/1901-2000/1931.Painting-a-Grid-With-Three-Different-Colors/Solution_test.go index 14ff50eb4..c517a6acf 100644 --- a/leetcode/1901-2000/1931.Painting-a-Grid-With-Three-Different-Colors/Solution_test.go +++ b/leetcode/1901-2000/1931.Painting-a-Grid-With-Three-Different-Colors/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + m, n int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 1, 1, 3}, + {"TestCase2", 1, 2, 6}, + {"TestCase3", 5, 5, 580986}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.m, c.n) 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.m, c.n) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }