Skip to content

Commit 36b83cf

Browse files
committed
Sync LeetCode submission Runtime - 3 ms (95.31%), Memory - 17.8 MB (41.41%)
1 parent a458514 commit 36b83cf

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<p>Given an integer <code>n</code>, find a sequence that satisfies all of the following:</p>
2+
3+
<ul>
4+
<li>The integer <code>1</code> occurs once in the sequence.</li>
5+
<li>Each integer between <code>2</code> and <code>n</code> occurs twice in the sequence.</li>
6+
<li>For every integer <code>i</code> between <code>2</code> and <code>n</code>, the <strong>distance</strong> between the two occurrences of <code>i</code> is exactly <code>i</code>.</li>
7+
</ul>
8+
9+
<p>The <strong>distance</strong> between two numbers on the sequence, <code>a[i]</code> and <code>a[j]</code>, is the absolute difference of their indices, <code>|j - i|</code>.</p>
10+
11+
<p>Return <em>the <strong>lexicographically largest</strong> sequence</em><em>. It is guaranteed that under the given constraints, there is always a solution. </em></p>
12+
13+
<p>A sequence <code>a</code> is lexicographically larger than a sequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, sequence <code>a</code> has a number greater than the corresponding number in <code>b</code>. For example, <code>[0,1,9,0]</code> is lexicographically larger than <code>[0,1,5,6]</code> because the first position they differ is at the third number, and <code>9</code> is greater than <code>5</code>.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> n = 3
20+
<strong>Output:</strong> [3,1,2,3,2]
21+
<strong>Explanation:</strong> [2,3,2,1,3] is also a valid sequence, but [3,1,2,3,2] is the lexicographically largest valid sequence.
22+
</pre>
23+
24+
<p><strong class="example">Example 2:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> n = 5
28+
<strong>Output:</strong> [5,3,1,4,3,5,2,4,2]
29+
</pre>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
<ul>
35+
<li><code>1 &lt;= n &lt;= 20</code></li>
36+
</ul>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Approach: Backtracking
2+
3+
# Time: O(n!)
4+
# Space: O(n)
5+
6+
class Solution:
7+
def constructDistancedSequence(self, n: int) -> List[int]:
8+
result_sequence = [0] * (n * 2 - 1)
9+
is_number_used = [False] * (n + 1)
10+
11+
self.find_lexicographically_largest_sequence(0, result_sequence, is_number_used, n)
12+
13+
return result_sequence
14+
15+
def find_lexicographically_largest_sequence(self, current_idx, result_sequence, is_number_used, n):
16+
if current_idx == len(result_sequence):
17+
return True
18+
19+
if result_sequence[current_idx] != 0:
20+
return self.find_lexicographically_largest_sequence(current_idx + 1, result_sequence, is_number_used, n)
21+
22+
for number_to_place in range(n, 0, -1):
23+
if is_number_used[number_to_place]:
24+
continue
25+
26+
is_number_used[number_to_place] = True
27+
result_sequence[current_idx] = number_to_place
28+
29+
# If placing number 1, move to the next index directly
30+
if number_to_place == 1:
31+
if self.find_lexicographically_largest_sequence(current_idx + 1, result_sequence, is_number_used, n):
32+
return True
33+
34+
# Place larger numbers at two positions if valid
35+
elif (current_idx + number_to_place < len(result_sequence) and result_sequence[current_idx + number_to_place] == 0):
36+
result_sequence[current_idx + number_to_place] = number_to_place
37+
38+
if self.find_lexicographically_largest_sequence(current_idx + 1, result_sequence, is_number_used, n):
39+
return True
40+
41+
# undo placement for backtracking
42+
result_sequence[current_idx + number_to_place] = 0
43+
44+
# Undo current placement and mark number as unused
45+
result_sequence[current_idx] = 0
46+
is_number_used[number_to_place] = False
47+
48+
return False

0 commit comments

Comments
 (0)