945. Three-Colour Grid Rows
A mosaic artist covers a wall of n rows and exactly 3 columns with square tiles. Each tile is painted red, green or blue, and two tiles that share a side (left-right in a row, or one above another) must never have the same colour.
Return the number of different ways to paint the whole wall, modulo 1_000_000_007 (10^9 + 7). Two paintings differ if at least one tile has a different colour. For n = 1 there are 12 ways.
Example 1
- Input:
- n = 2
- Output:
- 54
- Explanation:
A first row has 12 colourings: 6 of shape X-Y-X, each followed by 5 valid rows, and 6 of shape X-Y-Z, each followed by 4, giving 6*5 + 6*4 = 54.
Example 2
- Input:
- n = 4
- Output:
- 1122
- Explanation:
Repeating the row-to-row transition counts (3a + 2b, 2a + 2b) for the two row shapes yields 1122 valid paintings.
Constraints
- 1 ≤ n ≤ 105
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 800 msC++ 200 msJava 400 msJavaScript 400 msTypeScript 400 ms
Expected complexity
- Time
- O(n)
- Space
- O(1)