Skip to content

Commit 6858100

Browse files
committed
Add solution #2318
1 parent 2ed286a commit 6858100

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,884 LeetCode solutions in JavaScript
1+
# 1,885 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1760,6 +1760,7 @@
17601760
2315|[Count Asterisks](./solutions/2315-count-asterisks.js)|Easy|
17611761
2316|[Count Unreachable Pairs of Nodes in an Undirected Graph](./solutions/2316-count-unreachable-pairs-of-nodes-in-an-undirected-graph.js)|Medium|
17621762
2317|[Maximum XOR After Operations](./solutions/2317-maximum-xor-after-operations.js)|Medium|
1763+
2318|[Number of Distinct Roll Sequences](./solutions/2318-number-of-distinct-roll-sequences.js)|Hard|
17631764
2319|[Check if Matrix Is X-Matrix](./solutions/2319-check-if-matrix-is-x-matrix.js)|Easy|
17641765
2320|[Count Number of Ways to Place Houses](./solutions/2320-count-number-of-ways-to-place-houses.js)|Medium|
17651766
2336|[Smallest Number in Infinite Set](./solutions/2336-smallest-number-in-infinite-set.js)|Medium|
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* 2318. Number of Distinct Roll Sequences
3+
* https://leetcode.com/problems/number-of-distinct-roll-sequences/
4+
* Difficulty: Hard
5+
*
6+
* You are given an integer n. You roll a fair 6-sided dice n times. Determine the total number
7+
* of distinct sequences of rolls possible such that the following conditions are satisfied:
8+
* 1. The greatest common divisor of any adjacent values in the sequence is equal to 1.
9+
* 2. There is at least a gap of 2 rolls between equal valued rolls. More formally, if the
10+
* value of the ith roll is equal to the value of the jth roll, then abs(i - j) > 2.
11+
*
12+
* Return the total number of distinct sequences possible. Since the answer may be very large,
13+
* return it modulo 109 + 7.
14+
*
15+
* Two sequences are considered distinct if at least one element is different.
16+
*/
17+
18+
/**
19+
* @param {number} n
20+
* @return {number}
21+
*/
22+
var distinctSequences = function(n) {
23+
const MOD = 1e9 + 7;
24+
const gcd = (a, b) => b === 0 ? a : gcd(b, a % b);
25+
26+
const dp = new Array(n + 1)
27+
.fill()
28+
.map(() => new Array(7).fill().map(() => new Array(7).fill(0)));
29+
30+
for (let i = 1; i <= 6; i++) {
31+
dp[1][i][0] = 1;
32+
}
33+
34+
for (let len = 2; len <= n; len++) {
35+
for (let curr = 1; curr <= 6; curr++) {
36+
for (let prev = 0; prev <= 6; prev++) {
37+
for (let prevPrev = 0; prevPrev <= 6; prevPrev++) {
38+
if (dp[len - 1][prev][prevPrev] === 0) continue;
39+
if (curr === prev || curr === prevPrev) continue;
40+
if (prev !== 0 && gcd(curr, prev) !== 1) continue;
41+
dp[len][curr][prev] = (dp[len][curr][prev] + dp[len - 1][prev][prevPrev]) % MOD;
42+
}
43+
}
44+
}
45+
}
46+
47+
let result = 0;
48+
for (let curr = 1; curr <= 6; curr++) {
49+
for (let prev = 0; prev <= 6; prev++) {
50+
result = (result + dp[n][curr][prev]) % MOD;
51+
}
52+
}
53+
54+
return result;
55+
};

0 commit comments

Comments
 (0)