Skip to content

1931. Painting a Grid With Three Different Colors #1697

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to determine the number of ways to paint an m x n grid using three colors (red, green, and blue) such that no two adjacent cells (horizontally or vertically) have the same color. The result should be returned modulo 109 + 7.

Approach

  1. Generate Valid Column Configurations: First, generate all valid column configurations for an m-row grid where each cell in the column is colored such that no two consecutive cells have the same color. This can be done using backtracking to explore all possible valid color sequences.

  2. Precompute Compatible Transitions: For each valid column configuration, determine which other configurations can follow it such that no cells in the same row of consec…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit May 18, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim May 18, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested hard Difficulty
2 participants