-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJan_3(1411)
More file actions
51 lines (40 loc) · 1.23 KB
/
Copy pathJan_3(1411)
File metadata and controls
51 lines (40 loc) · 1.23 KB
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
class Solution {
public:
int M = 1e9 + 7;
vector<vector<int>> t;
//12 possible first rows
string states[12] = {"RYG", "RGY", "RYR", "RGR", "YRG", "YGR", "YGY", "YRY", "GRY", "GYR", "GRG", "GYG"};
int solve(int n, int prev) {
if(n == 0)
return 1;
if(t[n][prev] != -1)
return t[n][prev];
int result = 0;
string last = states[prev];
for(int curr = 0; curr < 12; curr++) {
if(curr == prev)
continue;
string currPat = states[curr];
bool conflict = false;
for(int col = 0; col < 3; col++) {
if(currPat[col] == last[col]) {
conflict = true;
break;
}
}
if(!conflict) {
result = (result + solve(n-1, curr)) % M;
}
}
return t[n][prev] = result;
}
int numOfWays(int n) {
t.resize(n, vector<int>(12, -1)); //T.C : O(n)
int result = 0;
for(int i = 0; i < 12; i++) { //chossing 1st row
//now remaining n-1 rows painting
result = (result + solve(n-1, i)) % M;
}
return result;
}
};