-
Notifications
You must be signed in to change notification settings - Fork 5
/
NumberOfWaysToPaintNX3Grid.java
53 lines (48 loc) · 1.52 KB
/
NumberOfWaysToPaintNX3Grid.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/*/
class Solution {
int n;
Integer[][][][] board;
public int numOfWays(int n) {
this.n = n;
board = new Integer[n][4][4][4];
return count(0,0,0,0);
}
private int count(int index, int color1, int color2, int color3)
{
if (index == n) return 1;
if (board[index][color1][color2][color3] != null) return board[index][color1][color2][color3];
int c1, c2, c3, result = 0;
for (c1 = 1; c1 <= 3; ++c1)
{
if (c1 != color1)
{
for (c2 = 1; c2 <= 3; ++c2)
{
if (c2 != color2 && c2 != c1)
{
for (c3 = 1; c3 <= 3; ++c3)
{
if (c3 != color3 && c3 != c2)
{
result = (result+count(index+1,c1,c2,c3))%((int)1e9+7);
}
}
}
}
}
}
return board[index][color1][color2][color3] = result;
}
}
class Solution {
public int numOfWays(int n) {
long a121 = 6, a123 = 6, b121, b123, mod = (long)1e9 + 7;
for (int i = 1; i < n; ++i) {
b121 = a121 * 3 + a123 * 2;
b123 = a121 * 2 + a123 * 2;
a121 = b121 % mod;
a123 = b123 % mod;
}
return (int)((a121 + a123) % mod);
}
}