Skip to content

Commit 68680a4

Browse files
authored
Merge pull request #898 from 0xff-dev/2318
Add solution and test-cases for problem 2318
2 parents 9fb5af9 + f2d4437 commit 68680a4

File tree

3 files changed

+85
-24
lines changed

3 files changed

+85
-24
lines changed
Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
11
# [2318.Number of Distinct Roll Sequences][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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:
5+
6+
1. The **greatest common divisor** of any **adjacent** values in the sequence is equal to `1`.
7+
2. There is **at least** a gap of 2 rolls between **equal** valued rolls. More formally, if the value of the i<sup>th</sup> roll is **equal** to the value of the j<sup>th</sup> roll, then `abs(i - j) > 2`.
8+
9+
Return the **total number** of distinct sequences possible. Since the answer may be very large, return it **modulo** `10^9 + 7`.
10+
11+
Two sequences are considered distinct if at least one element is different.
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
16+
Input: n = 4
17+
Output: 184
18+
Explanation: Some of the possible sequences are (1, 2, 3, 4), (6, 1, 2, 3), (1, 2, 3, 1), etc.
19+
Some invalid sequences are (1, 2, 1, 3), (1, 2, 3, 6).
20+
(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).
21+
(1, 2, 3, 6) is invalid since the greatest common divisor of 3 and 6 = 3.
22+
There are a total of 184 distinct sequences possible, so we return 184.
1323
```
1424

15-
## 题意
16-
> ...
25+
**Example 2:**
1726

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Number of Distinct Roll Sequences
23-
```go
2427
```
25-
28+
Input: n = 2
29+
Output: 22
30+
Explanation: Some of the possible sequences are (1, 2), (2, 1), (3, 2).
31+
Some invalid sequences are (3, 6), (2, 4) since the greatest common divisor is not equal to 1.
32+
There are a total of 22 distinct sequences possible, so we return 22.
33+
```
2634

2735
## 结语
2836

2937
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
30-
3138
[title]: https://leetcode.com/problems/number-of-distinct-roll-sequences/
3239
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,59 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
const mod2318 = 1000000007
4+
5+
func Solution(n int) int {
6+
cache := make([]map[int]map[[7]int]int, n+1)
7+
for i := 0; i <= n; i++ {
8+
cache[i] = map[int]map[[7]int]int{}
9+
for j := 1; j < 7; j++ {
10+
cache[i][j] = map[[7]int]int{}
11+
}
12+
}
13+
ref := map[int][]int{
14+
1: {1, 2, 3, 4, 5, 6},
15+
2: {1, 3, 5},
16+
3: {1, 2, 4, 5},
17+
4: {1, 3, 5},
18+
5: {1, 2, 3, 4, 6},
19+
6: {1, 5},
20+
}
21+
22+
var dfs func(int, int, [7]int) int
23+
dfs = func(nn, selected int, used [7]int) int {
24+
if nn <= 1 {
25+
return nn
26+
}
27+
m := cache[nn]
28+
if v, ok := m[selected]; ok {
29+
if v1, ok1 := v[used]; ok1 {
30+
return v1
31+
}
32+
}
33+
ans := 0
34+
for _, next := range ref[selected] {
35+
x := used
36+
if used[next] == 0 {
37+
used[next] = 2
38+
for j := 1; j < 7; j++ {
39+
if j == next || used[j] == 0 {
40+
continue
41+
}
42+
used[j]--
43+
}
44+
ans = (ans + dfs(nn-1, next, used)) % mod2318
45+
}
46+
used = x
47+
}
48+
cache[nn][selected][used] = ans
49+
return ans
50+
}
51+
r := 0
52+
for i := 1; i < 7; i++ {
53+
// 1:2
54+
used := [7]int{}
55+
used[i] = 2
56+
r = (r + dfs(n, i, used)) % mod2318
57+
}
58+
return r
559
}

leetcode/2301-2400/2318.Number-of-Distinct-Roll-Sequences/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 4, 184},
17+
{"TestCase2", 2, 22},
18+
{"TestCase3", 999, 209513669},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)