Skip to content

Commit 74c37a5

Browse files
authored
Merge pull request #1282 from 0xff-dev/1411
Add solution and test-cases for problem 1411
2 parents d9a3a2e + 42341b9 commit 74c37a5

File tree

4 files changed

+77
-9
lines changed

4 files changed

+77
-9
lines changed
24.2 KB
Loading
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# [1411.Number of Ways to Paint N × 3 Grid][title]
2+
3+
## Description
4+
You have a `grid` of size `n x 3` and you want to paint each cell of the grid with exactly one of the three colors: **Red**, **Yellow**, or **Green** while making sure that no two adjacent cells have the same color (i.e., no two cells that share vertical or horizontal sides have the same color).
5+
6+
Given `n` the number of rows of the `grid`, return the number of ways you can paint this grid. As the answer may grow large, the answer **must be** computed modulo `10^9 + 7`.
7+
8+
**Example 1:**
9+
10+
![1](./1.png)
11+
12+
```
13+
Input: n = 1
14+
Output: 12
15+
Explanation: There are 12 possible way to paint the grid as shown.
16+
```
17+
18+
**Example 2:**
19+
20+
```
21+
Input: n = 5000
22+
Output: 30228214
23+
```
24+
25+
## 结语
26+
27+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
28+
29+
[title]: https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid
30+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
const mod = 1000000007
4+
5+
func Solution(n int) int {
6+
rel := [12][]int{
7+
{1, 4, 10, 2, 5},
8+
{0, 3, 6, 8, 11},
9+
{0, 3, 6, 7},
10+
11+
{1, 7, 10, 2},
12+
{0, 6, 9, 8},
13+
{0, 6, 9, 7, 10},
14+
15+
{1, 4, 2, 5, 11},
16+
{3, 2, 5, 11},
17+
{9, 1, 4, 10},
18+
19+
{4, 5, 8, 11},
20+
{0, 3, 5, 8, 11},
21+
{1, 7, 10, 6, 9},
22+
}
23+
var dfs func(int, int) int
24+
cache := make(map[[2]int]int)
25+
dfs = func(parent, left int) int {
26+
if left == 0 {
27+
return 1
28+
}
29+
ans := 0
30+
l := len(rel[parent])
31+
key := [2]int{l, left}
32+
if v, ok := cache[key]; ok {
33+
return v
34+
}
35+
for _, next := range rel[parent] {
36+
ans = (ans + dfs(next, left-1)) % mod
37+
}
38+
cache[key] = ans
39+
return ans
40+
}
41+
a := (dfs(0, n-1) * 6) % mod
42+
b := (dfs(2, n-1) * 6) % mod
43+
return (a + b) % mod
544
}

leetcode/1401-1500/1411.Number-of-Ways-to-Paint-N-x-3-Grid/Solution_test.go

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

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

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

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

0 commit comments

Comments
 (0)