All problems
1032HardDynamic ProgrammingGraph Coloring

Colouring a Three-Wide Panel

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1411Number of Ways to Paint N × 3 Grid

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A panel is n rows tall and three cells wide. Each cell is to be painted red, yellow or green so that no two cells sharing an edge carry the same colour.

Return how many paintings there are, given as the remainder after dividing by 1000000007.

Examples

Example 1

Input
n = 2
Output
54

Twelve rows are legal on their own. Each of the six whose ends match allows five rows below it and each of the six with three different colours allows four, which comes to fifty-four two-row panels.

Example 2

Input
n = 6
Output
23346

Six rows deep, the two running tallies have been stepped down five times and add to twenty-three thousand three hundred and forty-six.

Example 3

Input
n = 3
Output
246

Three rows deep the count is two hundred and forty-six, small enough that the remainder never bites.

Constraints

  • 1 <= n <= 5000

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def num_of_ways(n: int) -> int:
Java
public int numOfWays(int n)
September 7
Apply