Similar Problems

Similar Problems not available

Number Of Ways To Paint N 3 Grid - Leetcode Solution

Companies:

LeetCode:  Number Of Ways To Paint N 3 Grid Leetcode Solution

Difficulty: Hard

Topics: dynamic-programming  

The problem Number Of Ways To Paint N×3 Grid can be solved using dynamic programming approach. The problem statement asks to calculate the number of ways to paint a 3xN grid using three colors - Red, Yellow and Green.

The approach to solve this problem can be broken down into subproblems of painting each column of the grid. For each column, there are four possible options:

  1. Paint it completely with Red
  2. Paint it completely with Yellow
  3. Paint it completely with Green
  4. Paint the column with a two-color pattern (RG, GY, etc)

For a given column, the total number of ways to paint it will be the sum of the above four options.

Now, let's define the dp state. Let dp[i][j] represents the number of ways to paint i columns of the grid where the last three columns are painted in a particular way. Here, i represents the ith column and j represents the pattern in which the last three columns are painted. j can have 4 possible values:

  1. j = 0: All last three columns of grid are painted in Red.
  2. j = 1: All last three columns of grid are painted in Yellow.
  3. j = 2: All last three columns of grid are painted in Green.
  4. j = 3: The last three columns of grid are painted in some two-color pattern.

We can then use this dp state to recursively calculate the total number of ways to paint a 3xN grid. We start by initializing the dp array with values for the base cases where i=0 and j can take any of the four values (0 to 3) and the corresponding number of ways is 1.

For i=1, we can simply calculate the number of ways for each possible j by taking the sum of number of ways for each of the four possible options for painting the ith column.

For i>1, the number of ways to paint the current column i will depend on the pattern of last three columns (j=0, j=1, j=2, j=3) and the pattern of last two columns in the previous row. We can precompute a table of valid patterns for two consecutive columns.

After computing the new dp state for i, we take the sum of the number of ways for columns i-2, i-1, and i and update the corresponding dp[i][j] values. Once we have computed dp[N][3], which represents the number of ways to paint N columns of the grid, we return the answer.

The time complexity of this approach is O(N) as we are simply iterating over N columns and updating the dp array for each possible pattern. The space complexity is O(1) as we are only using a constant amount of space to store our dp table.

Here is the Python implementation of the above approach:

class Solution:
    def numOfWays(self, n: int) -> int:
        dp = [[0]*4 for _ in range(n+1)]

        # Base cases
        for i in range(4):
            dp[0][i] = 1

        # Valid patterns for two columns in a row
        valid = {(0, 1), (0, 2), (0, 3), (1, 0), (1, 2), (2, 0), (2, 1), (2, 3), (3, 0), (3, 2)}

        for i in range(1, n+1):
            for j in range(4):
                for k in range(4):
                    if (j, k) in valid:
                        for l in range(4):
                            if (k, l) in valid:
                                dp[i][j] += dp[i-1][k]*dp[i-2][l]

        return sum(dp[n]) % (10**9 + 7)

Number Of Ways To Paint N 3 Grid Solution Code

1