diff --git a/leetcode/1401-1500/1411.Number-of-Ways-to-Paint-N-x-3-Grid/1.png b/leetcode/1401-1500/1411.Number-of-Ways-to-Paint-N-x-3-Grid/1.png new file mode 100644 index 000000000..33b7e2a32 Binary files /dev/null and b/leetcode/1401-1500/1411.Number-of-Ways-to-Paint-N-x-3-Grid/1.png differ diff --git a/leetcode/1401-1500/1411.Number-of-Ways-to-Paint-N-x-3-Grid/README.md b/leetcode/1401-1500/1411.Number-of-Ways-to-Paint-N-x-3-Grid/README.md new file mode 100644 index 000000000..2a0ca6a52 --- /dev/null +++ b/leetcode/1401-1500/1411.Number-of-Ways-to-Paint-N-x-3-Grid/README.md @@ -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 diff --git a/leetcode/1401-1500/1411.Number-of-Ways-to-Paint-N-x-3-Grid/Solution.go b/leetcode/1401-1500/1411.Number-of-Ways-to-Paint-N-x-3-Grid/Solution.go index d115ccf5e..e207db370 100755 --- a/leetcode/1401-1500/1411.Number-of-Ways-to-Paint-N-x-3-Grid/Solution.go +++ b/leetcode/1401-1500/1411.Number-of-Ways-to-Paint-N-x-3-Grid/Solution.go @@ -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 } diff --git a/leetcode/1401-1500/1411.Number-of-Ways-to-Paint-N-x-3-Grid/Solution_test.go b/leetcode/1401-1500/1411.Number-of-Ways-to-Paint-N-x-3-Grid/Solution_test.go index 14ff50eb4..ae9982b9b 100755 --- a/leetcode/1401-1500/1411.Number-of-Ways-to-Paint-N-x-3-Grid/Solution_test.go +++ b/leetcode/1401-1500/1411.Number-of-Ways-to-Paint-N-x-3-Grid/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }