LeetCode 1931: Painting a Grid With Three Different Colors Solution

Master LeetCode problem 1931 (Painting a Grid With Three Different Colors), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1931. Painting a Grid With Three Different Colors

Problem Explanation

Explanation:

To solve this problem, we can use dynamic programming. We will maintain a 2D array to keep track of the number of ways to color the grid at each step such that no two adjacent cells have the same color. We will iterate through each cell in the grid and calculate the number of valid colorings based on the colors of the adjacent cells.

At each step, we will consider the colors of the current cell and the previous column's cells to determine the valid colorings. We will keep track of the valid colorings for each color combination with a separate 2D array. Finally, we return the sum of valid colorings for the last column.

The time complexity of this solution is O(m * n * 3^m), where m and n are the dimensions of the grid, and the factor of 3^m accounts for the number of color combinations for each cell. The space complexity is O(m * 3^m) to store the valid colorings.

:

Solution Code

class Solution {
    public int colorTheGrid(int m, int n) {
        int MOD = 1000000007;
        int[][] dp = new int[n][1 << (2 * m)];
        
        for (int i = 0; i < 1 << m; i++) {
            dp[0][i] = 1;
        }
        
        for (int j = 1; j < n; j++) {
            for (int i = 0; i < 1 << (2 * m); i++) {
                int[] colors = new int[m];
                int temp = i;
                for (int k = 0; k < m; k++) {
                    colors[k] = temp % 4;
                    temp /= 4;
                }
                
                for (int i2 = 0; i2 < 1 << (2 * m); i2++) {
                    int[] colors2 = new int[m];
                    int temp2 = i2;
                    for (int k = 0; k < m; k++) {
                        colors2[k] = temp2 % 4;
                        temp2 /= 4;
                    }
                    
                    boolean valid = true;
                    for (int k = 0; k < m; k++) {
                        if ((colors[k] == colors2[k]) || (k > 0 && colors[k] == colors[k - 1] && colors2[k] == colors2[k - 1])) {
                            valid = false;
                            break;
                        }
                    }
                    
                    if (valid) {
                        dp[j][i] = (dp[j][i] + dp[j - 1][i2]) % MOD;
                    }
                }
            }
        }
        
        int result = 0;
        for (int i = 0; i < 1 << (2 * m); i++) {
            result = (result + dp[n - 1][i]) % MOD;
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1931 (Painting a Grid With Three Different Colors)?

This page provides optimized solutions for LeetCode problem 1931 (Painting a Grid With Three Different Colors) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1931 (Painting a Grid With Three Different Colors)?

The time complexity for LeetCode 1931 (Painting a Grid With Three Different Colors) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1931 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1931 in Java, C++, or Python.

Back to LeetCode Solutions