Skip to content

Commit 3d1c076

Browse files
committed
+ problem 1411
1 parent 45d130e commit 3d1c076

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# 1411. Number of Ways to Paint N × 3 Grid
2+
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).
3+
4+
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 <code>10<sup>9</sup> + 7</code>.
5+
6+
#### Example 1:
7+
![](https://assets.leetcode.com/uploads/2020/03/26/e1.png)
8+
<pre>
9+
<strong>Input:</strong> n = 1
10+
<strong>Output:</strong> 12
11+
<strong>Explanation:</strong> There are 12 possible way to paint the grid as shown.
12+
</pre>
13+
14+
#### Example 2:
15+
<pre>
16+
<strong>Input:</strong> n = 5000
17+
<strong>Output:</strong> 30228214
18+
</pre>
19+
20+
#### Constraints:
21+
* `n == grid.length`
22+
* `1 <= n <= 5000`
23+
24+
## Solutions (Rust)
25+
26+
### 1. Solution
27+
```Rust
28+
impl Solution {
29+
pub fn num_of_ways(n: i32) -> i32 {
30+
let mut x = 6;
31+
let mut y = 6;
32+
33+
for _ in 1..n {
34+
x = (x + y) % 1_000_000_007 * 2 % 1_000_000_007;
35+
y = (x + y) % 1_000_000_007;
36+
}
37+
38+
(x + y) % 1_000_000_007
39+
}
40+
}
41+
```
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# 1411. 给 N x 3 网格图涂色的方案数
2+
你有一个 `n x 3` 的网格图 `grid` ,你需要用 **红,黄,绿** 三种颜色之一给每一个格子上色,且确保相邻格子颜色不同(也就是有相同水平边或者垂直边的格子颜色不同)。
3+
4+
给你网格图的行数 `n`
5+
6+
请你返回给 `grid` 涂色的方案数。由于答案可能会非常大,请你返回答案对 `10^9 + 7` 取余的结果。
7+
8+
#### 示例 1:
9+
<pre>
10+
<strong>输入:</strong> n = 1
11+
<strong>输出:</strong> 12
12+
<strong>解释:</strong> 总共有 12 种可行的方法:
13+
<img src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/04/12/e1.png">
14+
</pre>
15+
16+
#### 示例 2:
17+
<pre>
18+
<strong>输入:</strong> n = 2
19+
<strong>输出:</strong> 54
20+
</pre>
21+
22+
#### 示例 3:
23+
<pre>
24+
<strong>输入:</strong> n = 3
25+
<strong>输出:</strong> 246
26+
</pre>
27+
28+
#### 示例 4:
29+
<pre>
30+
<strong>输入:</strong> n = 7
31+
<strong>输出:</strong> 106494
32+
</pre>
33+
34+
#### 示例 5:
35+
<pre>
36+
<strong>输入:</strong> n = 5000
37+
<strong>输出:</strong> 30228214
38+
</pre>
39+
40+
#### 提示:
41+
* `n == grid.length`
42+
* `grid[i].length == 3`
43+
* `1 <= n <= 5000`
44+
45+
## 题解 (Rust)
46+
47+
### 1. 题解
48+
```Rust
49+
impl Solution {
50+
pub fn num_of_ways(n: i32) -> i32 {
51+
let mut x = 6;
52+
let mut y = 6;
53+
54+
for _ in 1..n {
55+
x = (x + y) % 1_000_000_007 * 2 % 1_000_000_007;
56+
y = (x + y) % 1_000_000_007;
57+
}
58+
59+
(x + y) % 1_000_000_007
60+
}
61+
}
62+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn num_of_ways(n: i32) -> i32 {
3+
let mut x = 6;
4+
let mut y = 6;
5+
6+
for _ in 1..n {
7+
x = (x + y) % 1_000_000_007 * 2 % 1_000_000_007;
8+
y = (x + y) % 1_000_000_007;
9+
}
10+
11+
(x + y) % 1_000_000_007
12+
}
13+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@
846846
[1408][1408l]|[String Matching in an Array][1408] |![rs]
847847
[1409][1409l]|[Queries on a Permutation With Key][1409] |![rb]&nbsp;&nbsp;![rs]&nbsp;&nbsp;![kt]
848848
[1410][1410l]|[HTML Entity Parser][1410] |![rs]
849+
[1411][1411l]|[Number of Ways to Paint N × 3 Grid][1411] |![rs]
849850
[1413][1413l]|[Minimum Value to Get Positive Step by Step Sum][1413] |![rs]
850851
[1414][1414l]|[Find the Minimum Number of Fibonacci Numbers Whose Sum Is K][1414] |![rb]&nbsp;&nbsp;![rs]
851852
[1415][1415l]|[The k-th Lexicographical String of All Happy Strings of Length n][1415] |![py]&nbsp;&nbsp;![rb]
@@ -2273,6 +2274,7 @@
22732274
[1408]:Problemset/1408-String%20Matching%20in%20an%20Array/README.md#1408-string-matching-in-an-array
22742275
[1409]:Problemset/1409-Queries%20on%20a%20Permutation%20With%20Key/README.md#1409-queries-on-a-permutation-with-key
22752276
[1410]:Problemset/1410-HTML%20Entity%20Parser/README.md#1410-html-entity-parser
2277+
[1411]:Problemset/1411-Number%20of%20Ways%20to%20Paint%20N%20×%203%20Grid/README.md#1411-number-of-ways-to-paint-n-3-grid
22762278
[1413]:Problemset/1413-Minimum%20Value%20to%20Get%20Positive%20Step%20by%20Step%20Sum/README.md#1413-minimum-value-to-get-positive-step-by-step-sum
22772279
[1414]:Problemset/1414-Find%20the%20Minimum%20Number%20of%20Fibonacci%20Numbers%20Whose%20Sum%20Is%20K/README.md#1414-find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k
22782280
[1415]:Problemset/1415-The%20k-th%20Lexicographical%20String%20of%20All%20Happy%20Strings%20of%20Length%20n/README.md#1415-the-k-th-lexicographical-string-of-all-happy-strings-of-length-n
@@ -3699,6 +3701,7 @@
36993701
[1408l]:https://leetcode.com/problems/string-matching-in-an-array/
37003702
[1409l]:https://leetcode.com/problems/queries-on-a-permutation-with-key/
37013703
[1410l]:https://leetcode.com/problems/html-entity-parser/
3704+
[1411l]:https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/
37023705
[1413l]:https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/
37033706
[1414l]:https://leetcode.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k/
37043707
[1415l]:https://leetcode.com/problems/the-k-th-lexicographical-string-of-all-happy-strings-of-length-n/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@
846846
[1408][1408l]|[数组中的字符串匹配][1408] |![rs]
847847
[1409][1409l]|[查询带键的排列][1409] |![rb]&nbsp;&nbsp;![rs]&nbsp;&nbsp;![kt]
848848
[1410][1410l]|[HTML 实体解析器][1410] |![rs]
849+
[1411][1411l]|[给 N x 3 网格图涂色的方案数][1411] |![rs]
849850
[1413][1413l]|[逐步求和得到正数的最小值][1413] |![rs]
850851
[1414][1414l]|[和为 K 的最少斐波那契数字数目][1414] |![rb]&nbsp;&nbsp;![rs]
851852
[1415][1415l]|[长度为 n 的开心字符串中字典序第 k 小的字符串][1415] |![py]&nbsp;&nbsp;![rb]
@@ -2273,6 +2274,7 @@
22732274
[1408]:Problemset/1408-String%20Matching%20in%20an%20Array/README_CN.md#1408-数组中的字符串匹配
22742275
[1409]:Problemset/1409-Queries%20on%20a%20Permutation%20With%20Key/README_CN.md#1409-查询带键的排列
22752276
[1410]:Problemset/1410-HTML%20Entity%20Parser/README_CN.md#1410-html-实体解析器
2277+
[1411]:Problemset/1411-Number%20of%20Ways%20to%20Paint%20N%20×%203%20Grid/README_CN.md#1411-给-n-x-3-网格图涂色的方案数
22762278
[1413]:Problemset/1413-Minimum%20Value%20to%20Get%20Positive%20Step%20by%20Step%20Sum/README_CN.md#1413-逐步求和得到正数的最小值
22772279
[1414]:Problemset/1414-Find%20the%20Minimum%20Number%20of%20Fibonacci%20Numbers%20Whose%20Sum%20Is%20K/README_CN.md#1414-和为-k-的最少斐波那契数字数目
22782280
[1415]:Problemset/1415-The%20k-th%20Lexicographical%20String%20of%20All%20Happy%20Strings%20of%20Length%20n/README_CN.md#1415-长度为-n-的开心字符串中字典序第-k-小的字符串
@@ -3699,6 +3701,7 @@
36993701
[1408l]:https://leetcode.cn/problems/string-matching-in-an-array/
37003702
[1409l]:https://leetcode.cn/problems/queries-on-a-permutation-with-key/
37013703
[1410l]:https://leetcode.cn/problems/html-entity-parser/
3704+
[1411l]:https://leetcode.cn/problems/number-of-ways-to-paint-n-3-grid/
37023705
[1413l]:https://leetcode.cn/problems/minimum-value-to-get-positive-step-by-step-sum/
37033706
[1414l]:https://leetcode.cn/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k/
37043707
[1415l]:https://leetcode.cn/problems/the-k-th-lexicographical-string-of-all-happy-strings-of-length-n/

0 commit comments

Comments
 (0)