Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Painting a Grid With Three Different Colors

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1931" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, 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 are given two integers m and n. Consider an m x n grid where each cell is initially white. You can paint each cell red, green, or blue. All cells must be painted. Return the number of ways to color the grid with no two adjacent cells having the same color. Since the answer can be very large, return it modulo 109 + 7. Example 1: Input: m = 1, n = 1 Output: 3 Explanation: The three possible colorings are shown in the image above. Example 2: Input: m = 1, n = 2 Output: 6 Explanation: The six possible colorings are shown in the image above. Example 3: Input: m = 5, n = 5 Output: 580986 Constraints: 1 <= m <= 5 1 <= n <= 1000

Explanation

Here's a breakdown of the approach and the Python code:

  • Dynamic Programming: We use dynamic programming to solve this problem. We build up solutions for smaller subgrids to solve the larger grid. We keep track of the number of valid colorings for each row, given the colorings of the previous row.

  • Representing Colorings: Since m is small (<= 5), we can represent the coloring of a row as a single integer. Each digit in the base-3 representation of the integer represents the color (0, 1, or 2) of a cell in that row.

  • Transitions: For each row, we iterate through all possible valid colorings. A coloring is valid if no adjacent cells in the same row have the same color. Then, we check if it's compatible with the colorings of the previous row. Two rows are compatible if no two cells in the same column have the same color. We sum the number of ways to color the previous row to compute the number of ways to color the current row using dynamic programming.

  • Runtime Complexity: O(n 3^(2m)), where n is the number of columns and m* is the number of rows.

  • Storage Complexity: O(3^m), as we only need to store the counts for the current and previous rows.

Code

    def colorTheGrid(m: int, n: int) -> int:
    MOD = 10**9 + 7

    def get_valid_colorings(m: int):
        valid_colorings = []
        for i in range(3**m):
            coloring = []
            temp = i
            for _ in range(m):
                coloring.append(temp % 3)
                temp //= 3

            is_valid = True
            for j in range(m - 1):
                if coloring[j] == coloring[j+1]:
                    is_valid = False
                    break

            if is_valid:
                valid_colorings.append(coloring)
        return valid_colorings

    valid_colorings = get_valid_colorings(m)
    num_valid = len(valid_colorings)

    dp = [0] * num_valid
    for i in range(num_valid):
        dp[i] = 1

    for _ in range(n - 1):
        new_dp = [0] * num_valid
        for i in range(num_valid):
            for j in range(num_valid):
                compatible = True
                for k in range(m):
                    if valid_colorings[i][k] == valid_colorings[j][k]:
                        compatible = False
                        break
                if compatible:
                    new_dp[i] = (new_dp[i] + dp[j]) % MOD
        dp = new_dp

    total_ways = 0
    for ways in dp:
        total_ways = (total_ways + ways) % MOD

    return total_ways

More from this blog

C

Chatmagic blog

2894 posts