Skip to content

Commit eacc417

Browse files
committed
[LeetCode Sync] Runtime - 255 ms (84.21%), Memory - 17.6 MB (100.00%)
1 parent 72c9230 commit eacc417

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<p>You are given two integers <code>m</code> and <code>n</code>. Consider an <code>m x n</code> grid where each cell is initially white. You can paint each cell <strong>red</strong>, <strong>green</strong>, or <strong>blue</strong>. All cells <strong>must</strong> be painted.</p>
2+
3+
<p>Return<em> the number of ways to color the grid with <strong>no two adjacent cells having the same color</strong></em>. Since the answer can be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/22/colorthegrid.png" style="width: 200px; height: 50px;" />
8+
<pre>
9+
<strong>Input:</strong> m = 1, n = 1
10+
<strong>Output:</strong> 3
11+
<strong>Explanation:</strong> The three possible colorings are shown in the image above.
12+
</pre>
13+
14+
<p><strong class="example">Example 2:</strong></p>
15+
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/22/copy-of-colorthegrid.png" style="width: 321px; height: 121px;" />
16+
<pre>
17+
<strong>Input:</strong> m = 1, n = 2
18+
<strong>Output:</strong> 6
19+
<strong>Explanation:</strong> The six possible colorings are shown in the image above.
20+
</pre>
21+
22+
<p><strong class="example">Example 3:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> m = 5, n = 5
26+
<strong>Output:</strong> 580986
27+
</pre>
28+
29+
<p>&nbsp;</p>
30+
<p><strong>Constraints:</strong></p>
31+
32+
<ul>
33+
<li><code>1 &lt;= m &lt;= 5</code></li>
34+
<li><code>1 &lt;= n &lt;= 1000</code></li>
35+
</ul>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution:
2+
def colorTheGrid(self, m: int, n: int) -> int:
3+
mod = 10**9 + 7
4+
# Hash mapping stores all valid coloration schemes for a single row that meet the requirements
5+
# The key represents mask, and the value represents the ternary string of mask (stored as a list)
6+
valid = dict()
7+
8+
# Enumerate masks that meet the requirements within the range [0, 3^m)
9+
for mask in range(3**m):
10+
color = list()
11+
mm = mask
12+
for i in range(m):
13+
color.append(mm % 3)
14+
mm //= 3
15+
if any(color[i] == color[i + 1] for i in range(m - 1)):
16+
continue
17+
valid[mask] = color
18+
19+
# Preprocess all (mask1, mask2) binary tuples, satisfying mask1 and mask2 When adjacent rows, the colors of the two cells in the same column are different
20+
adjacent = defaultdict(list)
21+
for mask1, color1 in valid.items():
22+
for mask2, color2 in valid.items():
23+
if not any(x == y for x, y in zip(color1, color2)):
24+
adjacent[mask1].append(mask2)
25+
26+
f = [int(mask in valid) for mask in range(3**m)]
27+
for i in range(1, n):
28+
g = [0] * (3**m)
29+
for mask2 in valid.keys():
30+
for mask1 in adjacent[mask2]:
31+
g[mask2] += f[mask1]
32+
if g[mask2] >= mod:
33+
g[mask2] -= mod
34+
f = g
35+
36+
return sum(f) % mod

0 commit comments

Comments
 (0)