Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit77091a2

Browse files
authored
Added task 2318.
1 parent4cb33be commit77091a2

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+
packageg2301_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+
publicclassSolution {
6+
privateint[][][]memo =newint[10001][7][7];
7+
privateintmod =1000000007;
8+
privateint[][]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+
publicintdistinctSequences(intn) {
19+
returndp(n,0,0);
20+
}
21+
22+
privateintdp(intn,intprev,intpprev) {
23+
if (n ==0) {
24+
return1;
25+
}
26+
if (memo[n][prev][pprev] !=0) {
27+
returnmemo[n][prev][pprev];
28+
}
29+
intans =0;
30+
for (intx :m[prev]) {
31+
if (x !=pprev) {
32+
ans = (ans +dp(n -1,x,prev)) %mod;
33+
}
34+
}
35+
memo[n][prev][pprev] =ans;
36+
returnans;
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+
packageg2301_2400.s2318_number_of_distinct_roll_sequences;
2+
3+
importstaticorg.hamcrest.CoreMatchers.equalTo;
4+
importstaticorg.hamcrest.MatcherAssert.assertThat;
5+
6+
importorg.junit.jupiter.api.Test;
7+
8+
classSolutionTest {
9+
@Test
10+
voiddistinctSequences() {
11+
assertThat(newSolution().distinctSequences(4),equalTo(184));
12+
}
13+
14+
@Test
15+
voiddistinctSequences2() {
16+
assertThat(newSolution().distinctSequences(2),equalTo(22));
17+
}
18+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp