You have a grid of size n x 3 and you want to paint each cell of the grid with exactly one of the three colours: Red, Yellow or Green while making sure that no two adjacent cells have the same colour (i.e no two cells that share vertical or horizontal sides have the same colour).
You are given n the number of rows of the grid.
Return the number of ways you can paint this grid. As the answer may grow large, the answer must be computed modulo 10^9 + 7.
Example 1:
Input: n = 1
Output: 12
Explanation: There are 12 possible way to paint the grid as shown:
Example 2:
Input: n = 2
Output: 54
Example 3:
Input: n = 3
Output: 246
Example 4:
Input: n = 7
Output: 106494
Example 5:
Input: n = 5000
Output: 30228214
Constraints:
n == grid.length
grid[i].length == 3
1 <= n <= 5000
Solution:
class Solution {
public int numOfWays(int n) {
// ABA ABC
// 2c BAB BAB
// BCB BCB
// CAC
// 3c BAC BCA
// CAB CAB
int mod = (int) 1e9 + 7;
long color2 = 6;
long color3 = 6;
for (int i = 2; i <= n; i ++) {
long prevColor2 = color2;
color2 = (3 * color2 + 2 * color3) % mod;
color3 = (2 * color3 + 2 * prevColor2) % mod;
}
return (int) ((color2 + color3) % mod);
}
}