1579. Number of Ways to Paint N × 3 Grid
Difficulty: Hard
Topics: Dynamic Programming, Weekly Contest 184
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).
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 10⁹ + 7.
Example 1:
- Input: n = 1
- Output: 12
- Explanation: There are 12 possible way to paint the grid as shown.
Example 2:
- Input: n = 5000
- Output: 30228214
Constraints:
n == grid.length1 <= n <= 5000
Hint:
- We will use Dynamic programming approach. we will try all possible configuration.
- Let
dp[idx][prev1col][prev2col][prev3col]be the number of ways to color the rows of the grid from idx to n-1 keeping in mind that the previous row (idx - 1) has colors prev1col, prev2col and prev3col. Build the dp array to get the answer.
Solution:
We are given an n x 3 grid. We need to paint each cell with one of three colors (Red, Yellow, Green) such that no two adjacent cells (sharing a side) have the same color.
Approach:
Generate Valid Row Patterns
Each row of the grid has 3 cells, and adjacent cells in the same row must have different colors. With 3 colors, there are exactly 12 valid color patterns for a row. These are generated by considering all permutations of colors (0,1,2) where the first and second cells differ, and the second and third cells differ (the first and third may be the same).Build Transition Matrix
For any two row patterns, check if they can be placed in adjacent rows. This requires that for every column, the colors in the two rows are different. A 12×12 binary matrixtransis constructed, wheretrans[i][j] = 1if patterni(previous row) and patternj(current row) are compatible.-
Dynamic Programming
Letdp[j]be the number of ways to paint the grid up to the current row, ending with patternjin the current row.- For the first row,
dp[j] = 1for all 12 patterns. - For each subsequent row, compute a new array
next_dpwherenext_dp[j]is the sum ofdp[k]over all patternsksuch thattrans[k][j] = 1. - Update
dp = next_dpfor the next iteration. - The process repeats for
n-1rows (since the first row is already initialized).
- For the first row,
Result
After processing all rows, the answer is the sum ofdp[j]over all patternsj, taken modulo 10⁹ + 7.
Let's implement this solution in PHP: 1579. Number of Ways to Paint N × 3 Grid
<?php
/**
* @param Integer $n
* @return Integer
*/
function numOfWays($n) {
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo numOfWays(1) . "\n"; // Output: 12
echo numOfWays(5000) . "\n"; // Output: 30228214
?>
Explanation:
- The problem reduces to counting sequences of row patterns where adjacent rows are compatible.
- Each row pattern is a triple of colors representing the three cells in that row.
- Compatibility between rows is determined column-wise: colors in the same column must differ.
- Dynamic programming efficiently accumulates the number of valid ways row by row, using the transition matrix to ensure compatibility.
- The time complexity is O(n . 12²) = O(144n), which is efficient for n ≤ 5000.
Complexity
- Time Complexity: O(n . 12²) = O(144n), which is linear in n and efficient for n ≤ 5000.
- Space Complexity: O(12²) for the transition matrix and O(12) for the DP arrays, i.e., constant space.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

Top comments (0)