# Solving Leetcode Interviews in Seconds with AI: Number of Ways to Paint N × 3 Grid


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "1411" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like [Chatmagic](https://www.chatmagic.app), we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

	# Problem Statement
	> 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 109 + 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.length 1 <= n <= 5000  

	# Explanation
	Here's the breakdown of the solution:

*   **Dynamic Programming:** We use dynamic programming to solve this problem. We define two types of valid colorings for each row: `type1` (where the first and last columns have the same color) and `type2` (where the first and last columns have different colors).

*   **Transitions:** We calculate the number of ways to form valid colorings for the next row based on the number of ways to form valid colorings for the current row.

*   **Modulo Arithmetic:** We apply the modulo operator at each step to prevent integer overflow.

*   **Time and Space Complexity:**
    *   Time Complexity: O(n)
    *   Space Complexity: O(1)

	
	# Code
	```python
	def colorTheGrid(n: int) -> int:
    """
    Calculates the number of ways to color a 3 x n grid with 3 colors such that no two adjacent cells have the same color.

    Args:
        n: The number of rows in the grid.

    Returns:
        The number of ways to paint the grid modulo 10^9 + 7.
    """

    MOD = 10**9 + 7

    # Initial values for n = 1
    type1 = 6  # Number of ways where the first and last columns have the same color
    type2 = 6  # Number of ways where the first and last columns have different colors

    for _ in range(2, n + 1):
        new_type1 = (2 * type1 + 2 * type2) % MOD
        new_type2 = (2 * type1 + 3 * type2) % MOD
        type1 = new_type1
        type2 = new_type2

    return (type1 + type2) % MOD
	```
			
