<p>You have a <code>grid</code> of size <code>n x 3</code> and you want to paint each cell of the grid with exactly one of the three colors: <strong>Red</strong>, <strong>Yellow,</strong> or <strong>Green</strong> while making sure that no two adjacent cells have the same color (i.e., no two cells that share vertical or horizontal sides have the same color).</p>

<p>Given <code>n</code> the number of rows of the grid, return <em>the number of ways</em> you can paint this <code>grid</code>. As the answer may grow large, the answer <strong>must be</strong> computed modulo <code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/03/26/e1.png" style="width: 400px; height: 257px;" />
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 12
<strong>Explanation:</strong> There are 12 possible way to paint the grid as shown.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 5000
<strong>Output:</strong> 30228214
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == grid.length</code></li>
	<li><code>1 &lt;= n &lt;= 5000</code></li>
</ul>

class Solution {
public:

    int validRow(int c1, int c2, int c3) {
        return !(c1 == c2 or c2 == c3);
    }
    int numOfWays(int n) {
        // A[contents of previous row][n]
        // A[i][j] = sum over all valid contents of the current row, m: A[m][j - 1]

        // we have that there are at most 27 possible configurations of a single row.

        // 27 * n different subproblems, each sub problem should take, at most O(27) = O(1) time to compute

        // from the number denoting a row config, we can deduce the sequence of colours that made up that config by taking colour 1 = i % 3, colour 2 = (i // 3) % 3, colour 3 == (i // 9) % 3
        
        // A[0][config] = 1

        int mod = pow(10, 9) + 7;

        vector<vector<int>> A(n + 1, vector<int>(27, 1));

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 27; j++) {
                int c1 = j % 3;
                int c2 = (j / 3) % 3;
                int c3 = (j / 9) % 3;

                if (!validRow(c1, c2, c3)) {
                    A[i][j] = 0;
                    continue;
                }

                if (i == 0) {
                    continue;
                }

                // we summate possibilities for every valid cur row configuration
                int sum = 0;

                for (int config = 0; config < 27; config++) {
                    int curC1 = config % 3;
                    int curC2 = (config / 3) % 3;
                    int curC3 = (config / 9) % 3;

                    // check if valid row
                    if (!validRow(curC1, curC2, curC3)) {
                        continue;
                    }
                    
                    if (curC1 != c1 and curC2 != c2 and curC3 != c3) {
                        sum = (sum + A[i - 1][config]) % mod;
                    }
                }
                A[i][j] = sum;

            }
        }

        // summate all possibilities for n row
        int ans = 0;
        for (int j = 0; j < 27; j++) {
            ans = (ans + A[n - 1][j]) % mod;
        }

        return ans;
    }
};
20015