Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    1931 - Painting a Grid With Three Different Colors\.

    Hard

    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 <code>10<sup>9</sup> + 7</code>.

    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 &lt;= m &lt;= 5

    • 1 &lt;= n &lt;= 1000

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
      public final static Integer P
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Integer colorTheGrid(Integer m, Integer n)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait