forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_552.java
32 lines (28 loc) · 1023 Bytes
/
_552.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
package com.fishercoder.solutions;
public class _552 {
public static class Solution1 {
/**
* credit: https://discuss.leetcode.com/topic/86526/improving-the-runtime-from-o-n-to-o-log-n
*/
public int checkRecord(int n) {
final int MOD = 1000000007;
int[][][] f = new int[n + 1][2][3];
f[0] = new int[][]{{1, 1, 1}, {1, 1, 1}};
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 3; k++) {
int val = f[i - 1][j][2]; // ...P
if (j > 0) {
val = (val + f[i - 1][j - 1][2]) % MOD; // ...A
}
if (k > 0) {
val = (val + f[i - 1][j][k - 1]) % MOD; // ...L
}
f[i][j][k] = val;
}
}
}
return f[n][1][2];
}
}
}