Skip to content

Commit 155fc2c

Browse files
committed
Add solution and test-cases for problem 1931
1 parent a01e89c commit 155fc2c

File tree

5 files changed

+93
-25
lines changed

5 files changed

+93
-25
lines changed
Loading
Loading

leetcode/1901-2000/1931.Painting-a-Grid-With-Three-Different-Colors/README.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
# [1931.Painting a Grid With Three Different Colors][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 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.
5+
6+
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`.
77

8-
**Example 1:**
8+
**Example 1:**
9+
10+
![1](./1.png)
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: m = 1, n = 1
14+
Output: 3
15+
Explanation: The three possible colorings are shown in the image above.
1316
```
1417

15-
## 题意
16-
> ...
18+
**Example 2:**
1719

18-
## 题解
20+
![2](./2.png)
1921

20-
### 思路1
21-
> ...
22-
Painting a Grid With Three Different Colors
23-
```go
22+
```
23+
Input: m = 1, n = 2
24+
Output: 6
25+
Explanation: The six possible colorings are shown in the image above.
2426
```
2527

28+
**Example 3:**
29+
30+
```
31+
Input: m = 5, n = 5
32+
Output: 580986
33+
```
2634

2735
## 结语
2836

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

3-
func Solution(x bool) bool {
4-
return x
3+
const mod1931 = 1e9 + 7
4+
5+
var colors = []byte{'R', 'G', 'B'}
6+
7+
func columns(m int, cur string, preColor byte) []string {
8+
ans := []string{}
9+
if m == 0 {
10+
ans = append(ans, cur)
11+
return ans
12+
}
13+
14+
for _, color := range colors {
15+
if color != preColor {
16+
ans = append(ans, columns(m-1, cur+string(color), color)...)
17+
}
18+
}
19+
return ans
20+
}
21+
func can(a, b string) bool {
22+
for i := 0; i < len(a); i++ {
23+
if a[i] == b[i] {
24+
return false
25+
}
26+
}
27+
return true
28+
}
29+
30+
func Solution(m int, n int) int {
31+
groups := columns(m, "", ' ')
32+
cache := make(map[string]map[int]int)
33+
var dfs func(int, string) int
34+
dfs = func(left int, cur string) int {
35+
if left == 0 {
36+
return 1
37+
}
38+
if v, ok := cache[cur]; ok {
39+
if c, ok1 := v[left]; ok1 {
40+
return c
41+
}
42+
}
43+
ans := 0
44+
for i := 0; i < len(groups); i++ {
45+
if !can(cur, groups[i]) {
46+
continue
47+
}
48+
ans = (ans + dfs(left-1, groups[i])) % mod1931
49+
}
50+
51+
v, ok := cache[cur]
52+
if !ok {
53+
v = map[int]int{}
54+
}
55+
v[left] = ans
56+
cache[cur] = v
57+
return ans
58+
}
59+
// 挑选n个
60+
cur := ""
61+
for i := 0; i < m; i++ {
62+
cur += " "
63+
}
64+
return dfs(n, cur)
565
}

leetcode/1901-2000/1931.Painting-a-Grid-With-Three-Different-Colors/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
m, n int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 1, 1, 3},
17+
{"TestCase2", 1, 2, 6},
18+
{"TestCase3", 5, 5, 580986},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.m, c.n)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.m, c.n)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)