bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1931. Painting a Grid With Three Different Colors

1931. Painting a Grid With Three Different Colors

Leetcode Solutions

Top-down DP with bit mask

  1. Define a recursive function colorTheGrid that takes parameters m, n, i, j, cur, and prev representing the number of rows, number of columns, current row, current column, current column's color state, and previous column's color state, respectively.
  2. If the current row index i equals m, recursively call colorTheGrid for the next column.
  3. If the current column index j equals n, return 1 as we have found a valid coloring.
  4. If we are at the start of a new column (i is 0) and the DP table has a stored result for the current state, return the stored result.
  5. For each possible color k, check if it is different from the color above (up) and the color to the left (left). If so, recursively call colorTheGrid for the next cell with updated cur.
  6. Sum the results of all valid recursive calls and store the result in the DP table if at the start of a new column.
  7. Return the result.
UML Thumbnail

-D DP + BitMask + Unordered Map

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...