diff --git a/leetcode/1701-1800/1718.Construct-the-Lexicographically-Largest-Valid-Sequence/README.md b/leetcode/1701-1800/1718.Construct-the-Lexicographically-Largest-Valid-Sequence/README.md index 69fd3ea90..5a76d0785 100755 --- a/leetcode/1701-1800/1718.Construct-the-Lexicographically-Largest-Valid-Sequence/README.md +++ b/leetcode/1701-1800/1718.Construct-the-Lexicographically-Largest-Valid-Sequence/README.md @@ -1,28 +1,32 @@ # [1718.Construct the Lexicographically Largest Valid Sequence][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +Given an integer `n`, find a sequence that satisfies all of the following: + +- The integer `1` occurs once in the sequence. +- Each integer between `2` and `n` occurs twice in the sequence. +- For every integer `i` between `2` and `n`, the **distance** between the two occurrences of `i` is exactly `i`. + +The **distance** between two numbers on the sequence, `a[i]` and `a[j]`, is the absolute difference of their indices, `|j - i|`. + +Return the **lexicographically largest** sequence. It is guaranteed that under the given constraints, there is always a solution. + +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`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: n = 3 +Output: [3,1,2,3,2] +Explanation: [2,3,2,1,3] is also a valid sequence, but [3,1,2,3,2] is the lexicographically largest valid sequence. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Construct the Lexicographically Largest Valid Sequence -```go ``` - +Input: n = 5 +Output: [5,3,1,4,3,5,2,4,2] +``` ## 结语 diff --git a/leetcode/1701-1800/1718.Construct-the-Lexicographically-Largest-Valid-Sequence/Solution.go b/leetcode/1701-1800/1718.Construct-the-Lexicographically-Largest-Valid-Sequence/Solution.go index d115ccf5e..0bb6290c2 100644 --- a/leetcode/1701-1800/1718.Construct-the-Lexicographically-Largest-Valid-Sequence/Solution.go +++ b/leetcode/1701-1800/1718.Construct-the-Lexicographically-Largest-Valid-Sequence/Solution.go @@ -1,5 +1,43 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(n int) []int { + l := 2*n - 1 + res := make([]int, l) + used := make([]bool, n+1) + var dfs func(int) bool + dfs = func(start int) bool { + if start == l { + return true + } + if res[start] != 0 { + return dfs(start + 1) + } + + for s := n; s >= 1; s-- { + if used[s] { + continue + } + res[start] = s + used[s] = true + if s == 1 { + if dfs(start + 1) { + return true + } + } else { + if end := start + s; end < l && res[end] == 0 { + res[end] = s + if dfs(start + 1) { + return true + } + res[end] = 0 + } + } + res[start] = 0 + used[s] = false + } + return false + } + + dfs(0) + return res } diff --git a/leetcode/1701-1800/1718.Construct-the-Lexicographically-Largest-Valid-Sequence/Solution_test.go b/leetcode/1701-1800/1718.Construct-the-Lexicographically-Largest-Valid-Sequence/Solution_test.go index 14ff50eb4..23ba78394 100644 --- a/leetcode/1701-1800/1718.Construct-the-Lexicographically-Largest-Valid-Sequence/Solution_test.go +++ b/leetcode/1701-1800/1718.Construct-the-Lexicographically-Largest-Valid-Sequence/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 3, []int{3, 1, 2, 3, 2}}, + {"TestCase2", 5, []int{5, 3, 1, 4, 3, 5, 2, 4, 2}}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }