Skip to content

Add solution and test-cases for problem 1411 #1282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# [1411.Number of Ways to Paint N × 3 Grid][title]

## Description
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).

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`.

**Example 1:**

![1](./1.png)

```
Input: n = 1
Output: 12
Explanation: There are 12 possible way to paint the grid as shown.
```

**Example 2:**

```
Input: n = 5000
Output: 30228214
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
package Solution

func Solution(x bool) bool {
return x
const mod = 1000000007

func Solution(n int) int {
rel := [12][]int{
{1, 4, 10, 2, 5},
{0, 3, 6, 8, 11},
{0, 3, 6, 7},

{1, 7, 10, 2},
{0, 6, 9, 8},
{0, 6, 9, 7, 10},

{1, 4, 2, 5, 11},
{3, 2, 5, 11},
{9, 1, 4, 10},

{4, 5, 8, 11},
{0, 3, 5, 8, 11},
{1, 7, 10, 6, 9},
}
var dfs func(int, int) int
cache := make(map[[2]int]int)
dfs = func(parent, left int) int {
if left == 0 {
return 1
}
ans := 0
l := len(rel[parent])
key := [2]int{l, left}
if v, ok := cache[key]; ok {
return v
}
for _, next := range rel[parent] {
ans = (ans + dfs(next, left-1)) % mod
}
cache[key] = ans
return ans
}
a := (dfs(0, n-1) * 6) % mod
b := (dfs(2, n-1) * 6) % mod
return (a + b) % mod
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 1, 12},
{"TestCase2", 5000, 30228214},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading