Skip to content

Commit 77091a2

Browse files
authored
Added task 2318.
1 parent 4cb33be commit 77091a2

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.11'
14851485

14861486
| # | Title | Difficulty | Tag | Time, ms | Time, %
14871487
|------|----------------|-------------|-------------|----------|---------
1488+
| 2318 |[Number of Distinct Roll Sequences](src/main/java/g2301_2400/s2318_number_of_distinct_roll_sequences/Solution.java)| Hard || 254 | 91.67
14881489
| 2317 |[Maximum XOR After Operations](src/main/java/g2301_2400/s2317_maximum_xor_after_operations/Solution.java)| Medium || 1 | 100.00
14891490
| 2316 |[Count Unreachable Pairs of Nodes in an Undirected Graph](src/main/java/g2301_2400/s2316_count_unreachable_pairs_of_nodes_in_an_undirected_graph/Solution.java)| Medium || 32 | 100.00
14901491
| 2315 |[Count Asterisks](src/main/java/g2301_2400/s2315_count_asterisks/Solution.java)| Easy || 1 | 100.00
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g2301_2400.s2318_number_of_distinct_roll_sequences;
2+
3+
// #Hard #2022_06_26_Time_254_ms_(91.67%)_Space_51.6_MB_(58.33%)
4+
5+
public class Solution {
6+
private int[][][] memo = new int[10001][7][7];
7+
private int mod = 1000000007;
8+
private int[][] m = {
9+
{1, 2, 3, 4, 5, 6},
10+
{2, 3, 4, 5, 6},
11+
{1, 3, 5},
12+
{1, 2, 4, 5},
13+
{1, 3, 5},
14+
{1, 2, 3, 4, 6},
15+
{1, 5}
16+
};
17+
18+
public int distinctSequences(int n) {
19+
return dp(n, 0, 0);
20+
}
21+
22+
private int dp(int n, int prev, int pprev) {
23+
if (n == 0) {
24+
return 1;
25+
}
26+
if (memo[n][prev][pprev] != 0) {
27+
return memo[n][prev][pprev];
28+
}
29+
int ans = 0;
30+
for (int x : m[prev]) {
31+
if (x != pprev) {
32+
ans = (ans + dp(n - 1, x, prev)) % mod;
33+
}
34+
}
35+
memo[n][prev][pprev] = ans;
36+
return ans;
37+
}
38+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2318\. Number of Distinct Roll Sequences
2+
3+
Hard
4+
5+
You are given an integer `n`. You roll a fair 6-sided dice `n` times. Determine the total number of **distinct** sequences of rolls possible such that the following conditions are satisfied:
6+
7+
1. The **greatest common divisor** of any **adjacent** values in the sequence is equal to `1`.
8+
2. There is **at least** a gap of `2` rolls between **equal** valued rolls. More formally, if the value of the <code>i<sup>th</sup></code> roll is **equal** to the value of the <code>j<sup>th</sup></code> roll, then `abs(i - j) > 2`.
9+
10+
Return _the **total number** of distinct sequences possible_. Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
11+
12+
Two sequences are considered distinct if at least one element is different.
13+
14+
**Example 1:**
15+
16+
**Input:** n = 4
17+
18+
**Output:** 184
19+
20+
**Explanation:** Some of the possible sequences are (1, 2, 3, 4), (6, 1, 2, 3), (1, 2, 3, 1), etc.
21+
22+
Some invalid sequences are (1, 2, 1, 3), (1, 2, 3, 6).
23+
24+
(1, 2, 1, 3) is invalid since the first and third roll have an equal value and abs(1 - 3) = 2 (i and j are 1-indexed).
25+
26+
(1, 2, 3, 6) is invalid since the greatest common divisor of 3 and 6 = 3.
27+
28+
There are a total of 184 distinct sequences possible, so we return 184.
29+
30+
**Example 2:**
31+
32+
**Input:** n = 2
33+
34+
**Output:** 22
35+
36+
**Explanation:** Some of the possible sequences are (1, 2), (2, 1), (3, 2).
37+
38+
Some invalid sequences are (3, 6), (2, 4) since the greatest common divisor is not equal to 1.
39+
40+
There are a total of 22 distinct sequences possible, so we return 22.
41+
42+
**Constraints:**
43+
44+
* <code>1 <= n <= 10<sup>4</sup></code>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2301_2400.s2318_number_of_distinct_roll_sequences;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void distinctSequences() {
11+
assertThat(new Solution().distinctSequences(4), equalTo(184));
12+
}
13+
14+
@Test
15+
void distinctSequences2() {
16+
assertThat(new Solution().distinctSequences(2), equalTo(22));
17+
}
18+
}

0 commit comments

Comments
 (0)