Given an integer n, find a sequence that satisfies all of the following:
1 occurs once in the sequence.2 and n occurs twice in the sequence.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: 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:
Input: n = 5 Output: [5,3,1,4,3,5,2,4,2]
Constraints:
1 <= n <= 20This approach involves using a backtracking method to build the sequence from the largest number down to the smallest. We place numbers starting from n and try placing each number in positions that satisfy the distance requirement. If we find a valid placement for a given number, we continue to place the next smaller number. When all numbers are placed correctly, we have a solution. The constraint n ≤ 20 makes this approach feasible.
This Python solution uses a recursive backtracking approach to fill in the sequence from the largest number down to 1. The array `result` is used to store the current sequence configuration, and `used` keeps track of which numbers have been placed. The `backtrack` function attempts to place each number in allowed positions and recurses until a solution is found.
Time Complexity: O(n!) due to the combinatorial nature of backtracking.
Space Complexity: O(n) for the result and used arrays.
This approach constructs the sequence iteratively. It tries to place numbers from n down to 2 at allowed positions while ensuring the distance constraint is met. The challenge is to ensure placement satisfies both the distance and the need for the largest lexicographical order. The placement involves checking possible slots and filling iteratively.
This C++ solution iteratively places numbers from n to 2 with a main loop that attempts to place each number in its valid slots by checking indices. The innermost loop ensures that upon finding a valid slot, the number is placed, and iteration continues. Finally, the number 1 is placed in the first open slot.
Time Complexity: O(n^2) due to two nested loops traversing the positions.
Space Complexity: O(n) for storing the result sequence.
This approach breaks the problem into smaller, more manageable sub-problems, solves them individually, and combines their results.
One common example is the Merge Sort, where an array is continually split in half, each half is sorted, and then merged back together.
The above C code implements merge sort. The mergeSort function recursively divides the array into two halves and sorts each side. The merge function combines the sorted halves back into a continuous array.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n log n) due to the divide and conquer method where each level of recursion processes half-sized arrays.
Space Complexity: O(n) for temporary arrays used in the merge process.
This approach leans on using a heap data structure to facilitate sorting. Known as Heap Sort, it involves build-heap operations and sorting through the extract-max process repeatedly.
Commonly used for its in-place sort feature with reliable time complexities.
The given C code uses a heap-based sort algorithm. It builds a max heap from the array and sorts it by moving the maximum element to the end of the array and applying heapify repeatedly.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n log n) due to heapify operations.
Space Complexity: O(1) as heap sort is in-place.
| Approach | Complexity |
|---|---|
| Backtracking Solution | Time Complexity: O(n!) due to the combinatorial nature of backtracking. |
| Iterative Construction | Time Complexity: O(n^2) due to two nested loops traversing the positions. |
| Approach 1: Using Divide and Conquer | Time Complexity: O(n log n) due to the divide and conquer method where each level of recursion processes half-sized arrays. |
| Approach 2: Using a Heap-based Sort | Time Complexity: O(n log n) due to heapify operations. |
Construct the Lexicographically Largest Valid Sequence - Leetcode 1718 - Python • NeetCodeIO • 11,700 views views
Watch 9 more video solutions →Practice Construct the Lexicographically Largest Valid Sequence with our built-in code editor and test cases.
Practice on FleetCode