1718. Construct the Lexicographically Largest Valid Sequence Medium
1/**
2 * [1718] Construct the Lexicographically Largest Valid Sequence
3 *
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 * Return the lexicographically largest sequence. It is guaranteed that under the given constraints, there is always a solution.
12 * 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.
13 *
14 * Example 1:
15 *
16 * Input: n = 3
17 * Output: [3,1,2,3,2]
18 * Explanation: [2,3,2,1,3] is also a valid sequence, but [3,1,2,3,2] is the lexicographically largest valid sequence.
19 *
20 * Example 2:
21 *
22 * Input: n = 5
23 * Output: [5,3,1,4,3,5,2,4,2]
24 *
25 *
26 * Constraints:
27 *
28 * 1 <= n <= 20
29 *
30 */
31pub struct Solution {}
32
33// problem: https://leetcode.com/problems/construct-the-lexicographically-largest-valid-sequence/
34// discuss: https://leetcode.com/problems/construct-the-lexicographically-largest-valid-sequence/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38impl Solution {
39 pub fn construct_distanced_sequence(n: i32) -> Vec<i32> {
40 vec![]
41 }
42}
43
44// submission codes end
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn test_1718() {
52 }
53}
54
Back
© 2025 bowen.ge All Rights Reserved.