Leetcode 1411. Number of Ways to Paint N × 3 Grid

Title: Number of Ways to Paint a n X 3 grid


You are given a n X 3 grid, in which you must color each cell in one of three colors: Red, Yellow, or Green. However, no two cells that share a side (either horizontally or vertically) may be the same color. For a given number of rows, 'n', the task is to determine the total number of ways this grid can be painted. Due to the potential for very large numbers, the final total should then be returned modulo 10^9 + 7.


Let's look at n=2. The cells available in a 2 X 3 grid can be colored Red, Yellow and Green in such a way that no two adjacent cells are of the same color. We can calculate that there are altogether 54 different ways to paint this grid. Therefore, the output for n=2 should be 54.


The solution uses a simple yet effective dynamic programming approach that calculates the number of ways to paint this grid. The concept around the solution is the observation that a grid of 3 columns can have either 2 distinct colors or 3 distinct colors in each row (since no two adjacent cells can have the same color).

Therefore, we create two variables, 'color2' and 'color3' to hold the number of combinations for 2 distinct colors and 3 distinct colors respectively. We loop from 1 to 'n' to calculate combination of ways for each additional row.

The number of ways to color the next row with 2 colors is 3 times the number of ways to color the current row with 2 colors plus 2 times the number of ways to color the current row with 3 colors. Similarly, the number of ways to color the next row with 3 colors is 2 times the sum of the number of ways to color the current row with 2 or 3 colors.

Finally, we return the sum of 'color2' and 'color3' modulo 10^9 + 7.

Python Solution

3class Solution:
4    def numOfWays(self, n: int) -> int:
5        color2 = 6
6        color3 = 6
7        MOD = 10 ** 9 + 7
9        for _ in range(2, n + 1):
10            color2, color3 = (3 * color2 + 2 * color3) % MOD, (2 * color2 + 2 * color3) % MOD
12        return (color2 + color3) % MOD

Here, we use dynamic programming. color2 and color3 keep track of the number of ways to paint the grid so far, for 2 or 3 color configurations respectively.

Java Solution

3class Solution {
4    private static final int MOD = 1000000007;
6    public int numOfWays(int n) {
7        long color2 = 6;
8        long color3 = 6;
9        for (int i = 1; i < n; i++) {
10            long newColor2 = color2 * 3 + color3 * 2;
11            long newColor3 = color2 * 2 + color3 * 2;
12            color2 = newColor2 % MOD;
13            color3 = newColor3 % MOD;
14        }
15    return (int) (color2 + color3) % MOD;
16    }

Here, color2 and color3 update in each iteration of the grid, accumulating the total possible combinations.

C++ Solution

3class Solution {
5    int numOfWays(int n) {
6        int mod = pow(10,9)+7;
7        long color2 = 6;
8        long color3 = 6;
10        for (int i = 1; i < n; ++i) {
11            long tempColor3 = color3;
12            long tempColor2 = color2;
14            color3 = (2*tempColor3 + 2*tempColor2) % mod;
15            color2 = (3*tempColor2 + 2*tempColor3) % mod;
16        }
17        return (color3 + color2) % mod;
18    }

Same as in the Java solution, we keep track of the number of ways to paint the grid so far.

JavaScript Solution

3var numOfWays = function(n) {
4    let color2 = 6, color3 = 6;
5    const MOD = Math.pow(10,9)+7;
6    for (let i = 1; i < n; i++) {
7        let tempColor3 = color3;
8        let tempColor2 = color2;
9        color3 = (2*tempColor3 + 2*tempColor2) % MOD;
10        color2 = (3*tempColor2 + 2*tempColor3) % MOD;
11    }
12    return (color3 + color2) % MOD;

In JavaScript, we use the Math.pow function to calculate the number of ways, in the same way as described above.Javascript, Python, Java, and C++ are languages that offer versatility and robustness, their different syntax rules can sometimes create confusion with programmers transitioning from one to another. However, they are all powerful tools that are equally optimal for solving complex problems such as this one.

Overall, the key to understanding and solving this problem lies in the foundation of dynamic programming. The process involves finding patterns, remembering relevant past solutions, and using them to calculate new solutions. The specific algorithm explained here demonstrates how you can express the problem space in terms of two variables and then iteratively solve for larger and larger configurations of the grid. We model the problem using color2 and color3 to capture the number of possibilities for each type of row coloring we can have.

This approach provides a simple solution to a potentially complex issue. Remembering the number of ways to color configurations of rows and using that to calculate the number of ways to paint larger grids helps us avoid redundancy and keep the computational complexity manageable.

Lastly, it is essential to take common edge cases into account when dealing with computer programming problems. In this case, returning the result modulo 10^9 + 7 is a common requirement in many coding problems to prevent overflow and keep the results within the range of most common integer data types.

In conclusion, examining this problem through the lens of dynamic programming and the coloring problem introduces us to an effective strategy for reducing the problem space and increasing efficiency. This specific algorithm can be implemented in several programming languages like Python, Java, Javascript and C++, demonstrating the universal logic behind it.

Got a question? Ask the Teaching Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.

TA 👨‍🏫