Number of Distinct Roll Sequences LeetCode solution in Java
1class Solution {2static long[][] dp;34public int distinctSequences(int n) {5if (n == 1) return 6;6int mod = 1_000_000_007;7dp =8new long[][] {9{0, 1, 1, 1, 1, 1},10{1, 0, 1, 0, 1, 0},11{1, 1, 0, 1, 1, 0},12{1, 0, 1, 0, 1, 0},13{1, 1, 1, 1, 0, 1},14{1, 0, 0, 0, 1, 0}15};16for (int i = 2; i < n; i++) {17long[][] temp = new long[6][6];18for (int j = 0; j < 6; j++) {19for (int k = 0; k < 6; k++) {20long total = 0;21if (dp[j][k] == 0) continue;22for (int l = 0; l < 6; l++) {23total = (total + ((l == k) ? 0 : dp[l][j])) % mod;24}25temp[j][k] = total;26}27}28dp = temp;29}30long result = 0;31for (int i = 0; i < 6; i++) {32for (int j = 0; j < 6; j++) {33result = (result + dp[i][j]) % mod;34}35}36return (int) (result);37}38}