Skip to content

Commit 1c803c2

Browse files
committed
Add solution and test-cases for problem 1718
1 parent d569e0c commit 1c803c2

File tree

3 files changed

+64
-23
lines changed

3 files changed

+64
-23
lines changed

leetcode/1701-1800/1718.Construct-the-Lexicographically-Largest-Valid-Sequence/README.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
# [1718.Construct the Lexicographically Largest Valid Sequence][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+
Given an integer `n`, find a sequence that satisfies all of the following:
5+
6+
- The integer `1` occurs once in the sequence.
7+
- Each integer between `2` and `n` occurs twice in the sequence.
8+
- For every integer `i` between `2` and `n`, the **distance** between the two occurrences of `i` is exactly `i`.
9+
10+
The **distance** between two numbers on the sequence, `a[i]` and `a[j]`, is the absolute difference of their indices, `|j - i|`.
11+
12+
Return the **lexicographically largest** sequence. It is guaranteed that under the given constraints, there is always a solution.
13+
14+
A sequence `a` is lexicographically larger than a sequence `b` (of the same length) if in the first position where `a` and `b` differ, sequence a has a number greater than the corresponding number in `b`. For example, `[0,1,9,0]` is lexicographically larger than `[0,1,5,6]` because the first position they differ is at the third number, and `9` is greater than `5`.
715

816
**Example 1:**
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: n = 3
20+
Output: [3,1,2,3,2]
21+
Explanation: [2,3,2,1,3] is also a valid sequence, but [3,1,2,3,2] is the lexicographically largest valid sequence.
1322
```
1423

15-
## 题意
16-
> ...
17-
18-
## 题解
24+
**Example 2:**
1925

20-
### 思路1
21-
> ...
22-
Construct the Lexicographically Largest Valid Sequence
23-
```go
2426
```
25-
27+
Input: n = 5
28+
Output: [5,3,1,4,3,5,2,4,2]
29+
```
2630

2731
## 结语
2832

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int) []int {
4+
l := 2*n - 1
5+
res := make([]int, l)
6+
used := make([]bool, n+1)
7+
var dfs func(int) bool
8+
dfs = func(start int) bool {
9+
if start == l {
10+
return true
11+
}
12+
if res[start] != 0 {
13+
return dfs(start + 1)
14+
}
15+
16+
for s := n; s >= 1; s-- {
17+
if used[s] {
18+
continue
19+
}
20+
res[start] = s
21+
used[s] = true
22+
if s == 1 {
23+
if dfs(start + 1) {
24+
return true
25+
}
26+
} else {
27+
if end := start + s; end < l && res[end] == 0 {
28+
res[end] = s
29+
if dfs(start + 1) {
30+
return true
31+
}
32+
res[end] = 0
33+
}
34+
}
35+
res[start] = 0
36+
used[s] = false
37+
}
38+
return false
39+
}
40+
41+
dfs(0)
42+
return res
543
}

leetcode/1701-1800/1718.Construct-the-Lexicographically-Largest-Valid-Sequence/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ 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", 3, []int{3, 1, 2, 3, 2}},
17+
{"TestCase2", 5, []int{5, 3, 1, 4, 3, 5, 2, 4, 2}},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)