Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

001718 construct the lexicographically largest valid sequence #1324

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
201 commits
Select commit Hold shift + click to select a range
b225fc3
Automated commit 1
mah-shamim Feb 16, 2025
f440976
Automated commit rollback 1
mah-shamim Feb 16, 2025
70ce7c6
Automated commit 2
mah-shamim Feb 16, 2025
21eb90b
Automated commit rollback 2
mah-shamim Feb 16, 2025
f0ff366
Automated commit 3
mah-shamim Feb 16, 2025
ea95d26
Automated commit rollback 3
mah-shamim Feb 16, 2025
cfd521a
Automated commit 4
mah-shamim Feb 16, 2025
1514950
Automated commit rollback 4
mah-shamim Feb 16, 2025
c143409
Automated commit 5
mah-shamim Feb 16, 2025
d1b4a56
Automated commit rollback 5
mah-shamim Feb 16, 2025
e8d8da4
Automated commit 6
mah-shamim Feb 16, 2025
4e12290
Automated commit rollback 6
mah-shamim Feb 16, 2025
814e3b7
Automated commit 7
mah-shamim Feb 16, 2025
30fceea
Automated commit rollback 7
mah-shamim Feb 16, 2025
f8fb223
Automated commit 8
mah-shamim Feb 16, 2025
6281617
Automated commit rollback 8
mah-shamim Feb 16, 2025
7391771
Automated commit 9
mah-shamim Feb 16, 2025
ed7c587
Automated commit rollback 9
mah-shamim Feb 16, 2025
3b2a0b2
Automated commit 10
mah-shamim Feb 16, 2025
b86700a
Automated commit rollback 10
mah-shamim Feb 16, 2025
94d35d0
Automated commit 11
mah-shamim Feb 16, 2025
b1fbfc3
Automated commit rollback 11
mah-shamim Feb 16, 2025
487320d
Automated commit 12
mah-shamim Feb 16, 2025
f7d27c5
Automated commit rollback 12
mah-shamim Feb 16, 2025
bad6380
Automated commit 13
mah-shamim Feb 16, 2025
ebd9d39
Automated commit rollback 13
mah-shamim Feb 16, 2025
87ddd71
Automated commit 14
mah-shamim Feb 16, 2025
cc428c4
Automated commit rollback 14
mah-shamim Feb 16, 2025
7032826
Automated commit 15
mah-shamim Feb 16, 2025
437432a
Automated commit rollback 15
mah-shamim Feb 16, 2025
53b42f3
Automated commit 16
mah-shamim Feb 16, 2025
8962a14
Automated commit rollback 16
mah-shamim Feb 16, 2025
7a013ba
Automated commit 17
mah-shamim Feb 16, 2025
f87a4e8
Automated commit rollback 17
mah-shamim Feb 16, 2025
75ca97a
Automated commit 18
mah-shamim Feb 16, 2025
c92641f
Automated commit rollback 18
mah-shamim Feb 16, 2025
505907d
Automated commit 19
mah-shamim Feb 16, 2025
ab4d2ab
Automated commit rollback 19
mah-shamim Feb 16, 2025
74ae71a
Automated commit 20
mah-shamim Feb 16, 2025
3378103
Automated commit rollback 20
mah-shamim Feb 16, 2025
fc1e70c
Automated commit 21
mah-shamim Feb 16, 2025
00a7639
Automated commit rollback 21
mah-shamim Feb 16, 2025
5e945b8
Automated commit 22
mah-shamim Feb 16, 2025
31c0a6c
Automated commit rollback 22
mah-shamim Feb 16, 2025
99c2465
Automated commit 23
mah-shamim Feb 16, 2025
c5959a7
Automated commit rollback 23
mah-shamim Feb 16, 2025
0a3969a
Automated commit 24
mah-shamim Feb 16, 2025
7790d03
Automated commit rollback 24
mah-shamim Feb 16, 2025
f20de25
Automated commit 25
mah-shamim Feb 16, 2025
bbb5620
Automated commit rollback 25
mah-shamim Feb 16, 2025
ffa2287
Automated commit 26
mah-shamim Feb 16, 2025
ab9c70f
Automated commit rollback 26
mah-shamim Feb 16, 2025
97cd6fa
Automated commit 27
mah-shamim Feb 16, 2025
3dc84d8
Automated commit rollback 27
mah-shamim Feb 16, 2025
7efb331
Automated commit 28
mah-shamim Feb 16, 2025
9d642a5
Automated commit rollback 28
mah-shamim Feb 16, 2025
abaeed5
Automated commit 29
mah-shamim Feb 16, 2025
69248d3
Automated commit rollback 29
mah-shamim Feb 16, 2025
6d19158
Automated commit 30
mah-shamim Feb 16, 2025
c04cf72
Automated commit rollback 30
mah-shamim Feb 16, 2025
fb77b24
Automated commit 31
mah-shamim Feb 16, 2025
1589926
Automated commit rollback 31
mah-shamim Feb 16, 2025
641b53a
Automated commit 32
mah-shamim Feb 16, 2025
2893dde
Automated commit rollback 32
mah-shamim Feb 16, 2025
8b0ded8
Automated commit 33
mah-shamim Feb 16, 2025
7f17de2
Automated commit rollback 33
mah-shamim Feb 16, 2025
e0a940d
Automated commit 34
mah-shamim Feb 16, 2025
f94d2a2
Automated commit rollback 34
mah-shamim Feb 16, 2025
6cc6435
Automated commit 35
mah-shamim Feb 16, 2025
fdc4202
Automated commit rollback 35
mah-shamim Feb 16, 2025
2d20f23
Automated commit 36
mah-shamim Feb 16, 2025
1b74dde
Automated commit rollback 36
mah-shamim Feb 16, 2025
3ef1b03
Automated commit 37
mah-shamim Feb 16, 2025
a35ae48
Automated commit rollback 37
mah-shamim Feb 16, 2025
510486a
Automated commit 38
mah-shamim Feb 16, 2025
f118f1f
Automated commit rollback 38
mah-shamim Feb 16, 2025
a62429a
Automated commit 39
mah-shamim Feb 16, 2025
72c5f06
Automated commit rollback 39
mah-shamim Feb 16, 2025
f2304d8
Automated commit 40
mah-shamim Feb 16, 2025
4cd8d00
Automated commit rollback 40
mah-shamim Feb 16, 2025
26b6ac0
Automated commit 41
mah-shamim Feb 16, 2025
f46f8bb
Automated commit rollback 41
mah-shamim Feb 16, 2025
21587ac
Automated commit 42
mah-shamim Feb 16, 2025
e33c350
Automated commit rollback 42
mah-shamim Feb 16, 2025
67388bd
Automated commit 43
mah-shamim Feb 16, 2025
a1d0879
Automated commit rollback 43
mah-shamim Feb 16, 2025
050b57b
Automated commit 44
mah-shamim Feb 16, 2025
6263f65
Automated commit rollback 44
mah-shamim Feb 16, 2025
8fa8197
Automated commit 45
mah-shamim Feb 16, 2025
a66478f
Automated commit rollback 45
mah-shamim Feb 16, 2025
798222b
Automated commit 46
mah-shamim Feb 16, 2025
fd55e52
Automated commit rollback 46
mah-shamim Feb 16, 2025
9b55e40
Automated commit 47
mah-shamim Feb 16, 2025
6b13b55
Automated commit rollback 47
mah-shamim Feb 16, 2025
89a6709
Automated commit 48
mah-shamim Feb 16, 2025
23ba03a
Automated commit rollback 48
mah-shamim Feb 16, 2025
0717522
Automated commit 49
mah-shamim Feb 16, 2025
3428e6d
Automated commit rollback 49
mah-shamim Feb 16, 2025
a940d78
Automated commit 50
mah-shamim Feb 16, 2025
d32e16f
Automated commit rollback 50
mah-shamim Feb 16, 2025
9ef0439
Automated commit 51
mah-shamim Feb 16, 2025
4a8765a
Automated commit rollback 51
mah-shamim Feb 16, 2025
e3a36ca
Automated commit 52
mah-shamim Feb 16, 2025
720ffdc
Automated commit rollback 52
mah-shamim Feb 16, 2025
3e3f73d
Automated commit 53
mah-shamim Feb 16, 2025
3f2ff70
Automated commit rollback 53
mah-shamim Feb 16, 2025
d96b57b
Automated commit 54
mah-shamim Feb 16, 2025
51348fd
Automated commit rollback 54
mah-shamim Feb 16, 2025
3995930
Automated commit 55
mah-shamim Feb 16, 2025
d49540e
Automated commit rollback 55
mah-shamim Feb 16, 2025
61bf633
Automated commit 56
mah-shamim Feb 16, 2025
857ece2
Automated commit rollback 56
mah-shamim Feb 16, 2025
8eb0297
Automated commit 57
mah-shamim Feb 16, 2025
fa38c24
Automated commit rollback 57
mah-shamim Feb 16, 2025
d6a532b
Automated commit 58
mah-shamim Feb 16, 2025
fa86002
Automated commit rollback 58
mah-shamim Feb 16, 2025
87df2bd
Automated commit 59
mah-shamim Feb 16, 2025
7041e5f
Automated commit rollback 59
mah-shamim Feb 16, 2025
03a0186
Automated commit 60
mah-shamim Feb 16, 2025
cc823c0
Automated commit rollback 60
mah-shamim Feb 16, 2025
e06101c
Automated commit 61
mah-shamim Feb 16, 2025
237bdb8
Automated commit rollback 61
mah-shamim Feb 16, 2025
2f7190c
Automated commit 62
mah-shamim Feb 16, 2025
96bb779
Automated commit rollback 62
mah-shamim Feb 16, 2025
6316b97
Automated commit 63
mah-shamim Feb 16, 2025
5519ffa
Automated commit rollback 63
mah-shamim Feb 16, 2025
b334794
Automated commit 64
mah-shamim Feb 16, 2025
d0b39b0
Automated commit rollback 64
mah-shamim Feb 16, 2025
975df6d
Automated commit 65
mah-shamim Feb 16, 2025
80d6c18
Automated commit rollback 65
mah-shamim Feb 16, 2025
2fbd656
Automated commit 66
mah-shamim Feb 16, 2025
24c59fa
Automated commit rollback 66
mah-shamim Feb 16, 2025
5d04e12
Automated commit 67
mah-shamim Feb 16, 2025
4b97939
Automated commit rollback 67
mah-shamim Feb 16, 2025
caeadcf
Automated commit 68
mah-shamim Feb 16, 2025
e642830
Automated commit rollback 68
mah-shamim Feb 16, 2025
97613a0
Automated commit 69
mah-shamim Feb 16, 2025
7ee3451
Automated commit rollback 69
mah-shamim Feb 16, 2025
888b3ef
Automated commit 70
mah-shamim Feb 16, 2025
fc33f8f
Automated commit rollback 70
mah-shamim Feb 16, 2025
b3b357c
Automated commit 71
mah-shamim Feb 16, 2025
6e6ff85
Automated commit rollback 71
mah-shamim Feb 16, 2025
1e20730
Automated commit 72
mah-shamim Feb 16, 2025
9bd961d
Automated commit rollback 72
mah-shamim Feb 16, 2025
37f68c8
Automated commit 73
mah-shamim Feb 16, 2025
6d487a0
Automated commit rollback 73
mah-shamim Feb 16, 2025
e7b2ac8
Automated commit 74
mah-shamim Feb 16, 2025
782144c
Automated commit rollback 74
mah-shamim Feb 16, 2025
683ada5
Automated commit 75
mah-shamim Feb 16, 2025
d63e4cf
Automated commit rollback 75
mah-shamim Feb 16, 2025
5e3f610
Automated commit 76
mah-shamim Feb 16, 2025
f1bc199
Automated commit rollback 76
mah-shamim Feb 16, 2025
91dac0f
Automated commit 77
mah-shamim Feb 16, 2025
239fde2
Automated commit rollback 77
mah-shamim Feb 16, 2025
61ccf08
Automated commit 78
mah-shamim Feb 16, 2025
fbfe584
Automated commit rollback 78
mah-shamim Feb 16, 2025
19b72a3
Automated commit 79
mah-shamim Feb 16, 2025
f45e875
Automated commit rollback 79
mah-shamim Feb 16, 2025
8d36eab
Automated commit 80
mah-shamim Feb 16, 2025
a84b14a
Automated commit rollback 80
mah-shamim Feb 16, 2025
2226134
Automated commit 81
mah-shamim Feb 16, 2025
ec9fc0d
Automated commit rollback 81
mah-shamim Feb 16, 2025
fbd957b
Automated commit 82
mah-shamim Feb 16, 2025
73470bc
Automated commit rollback 82
mah-shamim Feb 16, 2025
0c1cc9f
Automated commit 83
mah-shamim Feb 16, 2025
99bd48c
Automated commit rollback 83
mah-shamim Feb 16, 2025
28c411b
Automated commit 84
mah-shamim Feb 16, 2025
ff85a44
Automated commit rollback 84
mah-shamim Feb 16, 2025
891d0a1
Automated commit 85
mah-shamim Feb 16, 2025
ea24df6
Automated commit rollback 85
mah-shamim Feb 16, 2025
191ea52
Automated commit 86
mah-shamim Feb 16, 2025
4eafbf0
Automated commit rollback 86
mah-shamim Feb 16, 2025
add73c6
Automated commit 87
mah-shamim Feb 16, 2025
1e38829
Automated commit rollback 87
mah-shamim Feb 16, 2025
b90fae6
Automated commit 88
mah-shamim Feb 16, 2025
e3b59ab
Automated commit rollback 88
mah-shamim Feb 16, 2025
31a7cfb
Automated commit 89
mah-shamim Feb 16, 2025
47f121c
Automated commit rollback 89
mah-shamim Feb 16, 2025
69a30e6
Automated commit 90
mah-shamim Feb 16, 2025
1f93ab7
Automated commit rollback 90
mah-shamim Feb 16, 2025
9e6c61b
Automated commit 91
mah-shamim Feb 16, 2025
8909976
Automated commit rollback 91
mah-shamim Feb 16, 2025
0be8e1c
Automated commit 92
mah-shamim Feb 16, 2025
fa825d9
Automated commit rollback 92
mah-shamim Feb 16, 2025
b1396c5
Automated commit 93
mah-shamim Feb 16, 2025
7c4e62e
Automated commit rollback 93
mah-shamim Feb 16, 2025
d974435
Automated commit 94
mah-shamim Feb 16, 2025
d3b5015
Automated commit rollback 94
mah-shamim Feb 16, 2025
9646ff5
Automated commit 95
mah-shamim Feb 16, 2025
e768844
Automated commit rollback 95
mah-shamim Feb 16, 2025
abee98c
Automated commit 96
mah-shamim Feb 16, 2025
acff6b3
Automated commit rollback 96
mah-shamim Feb 16, 2025
0df301a
Automated commit 97
mah-shamim Feb 16, 2025
8953caf
Automated commit rollback 97
mah-shamim Feb 16, 2025
eff80c7
Automated commit 98
mah-shamim Feb 16, 2025
636c83b
Automated commit rollback 98
mah-shamim Feb 16, 2025
01b4b54
Automated commit 99
mah-shamim Feb 16, 2025
08446a4
Automated commit rollback 99
mah-shamim Feb 16, 2025
3b04fcb
Automated commit 100
mah-shamim Feb 16, 2025
9e1d4bb
Automated commit rollback 100
mah-shamim Feb 16, 2025
51437e5
#1322, #1323 leetcode problems 001718-construct-the-lexicographically…
mah-shamim Feb 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
1718\. Construct the Lexicographically Largest Valid Sequence

**Difficulty:** Medium

**Topics:** `Array`, `Backtracking`

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:** 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 <= 20`


**Hint:**
1. Heuristic algorithm may work.



**Solution:**

We need to construct the lexicographically largest valid sequence that satisfies specific constraints. The sequence must include the integer 1 once and each integer from 2 to n exactly twice, with the distance between their occurrences equal to their value.

### Approach
The approach uses a backtracking algorithm to build the sequence step-by-step, ensuring that each number is placed in the highest possible position to achieve the lexicographically largest sequence. The key steps are:

1. **Track Usage and Reservations**: Use arrays to track which numbers have been used and their positions, and which positions are reserved for subsequent occurrences of a number.
2. **Backtracking**: Recursively attempt to place each number starting from the largest possible value (n) down to 1. This ensures that the lexicographically largest sequence is found first.
3. **Reserve Positions**: When placing the first occurrence of a number, reserve the position where the second occurrence must be placed. This ensures the correct distance between the two occurrences.

Let's implement this solution in PHP: **[1718. Construct the Lexicographically Largest Valid Sequence](https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/001718-construct-the-lexicographically-largest-valid-sequence/solution.php)**

```php
<?php
/**
* @param Integer $n
* @return Integer[]
*/
function constructDistancedSequence($n) {
...
...
...
/**
* go to ./solution.php
*/
}

/**
* @param $current_pos
* @param $sequence
* @param $used
* @param $reserved
* @param $n
* @return bool
*/
function backtrack($current_pos, &$sequence, &$used, &$reserved, $n) {
...
...
...
/**
* go to ./solution.php
*/
}

// Test cases
print_r(constructDistancedSequence(3));
print_r(constructDistancedSequence(5));
?>
```

### Explanation:

1. **Initialization**: The `constructLargestValidSequence` function initializes the necessary arrays and starts the backtracking process from position 0.
2. **Backtracking Function**: The `backtrack` function handles placing numbers in the sequence:
- If the current position is reserved, it places the required number and checks if the rest of the sequence can be filled.
- If not reserved, it tries placing numbers from n down to 1, ensuring the largest possible number is placed first.
- For each number, it checks if the next occurrence can be placed at the correct reserved position, ensuring the distance constraint is met.

This approach efficiently explores possible sequences while ensuring the lexicographically largest valid sequence is found first.

**Contact Links**

If you found this series helpful, please consider giving the **[repository](https://github.com/mah-shamim/leet-code-in-php)** a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

- **[LinkedIn](https://www.linkedin.com/in/arifulhaque/)**
- **[GitHub](https://github.com/mah-shamim)**
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

class Solution {

/**
* @param Integer $n
* @return Integer[]
*/
function constructDistancedSequence($n) {
$length = 2 * $n - 1;
$sequence = array_fill(0, $length, 0);
$used = array_fill(0, $n + 1, 0); // used[0] unused
$reserved = array_fill(0, $length, null);

$success = $this->backtrack(0, $sequence, $used, $reserved, $n);

return $success ? $sequence : [];
}

/**
* @param $current_pos
* @param $sequence
* @param $used
* @param $reserved
* @param $n
* @return bool
*/
function backtrack($current_pos, &$sequence, &$used, &$reserved, $n) {
$length = 2 * $n - 1;
if ($current_pos >= $length) {
return true;
}

if ($sequence[$current_pos] != 0) {
return $this->backtrack($current_pos + 1, $sequence, $used, $reserved, $n);
}

if ($reserved[$current_pos] !== null) {
$num = $reserved[$current_pos];
if ($used[$num] != 1) {
return false;
}
// Place the second occurrence
$sequence[$current_pos] = $num;
$used[$num] = 2;
$success = $this->backtrack($current_pos + 1, $sequence, $used, $reserved, $n);
if ($success) {
return true;
} else {
$sequence[$current_pos] = 0;
$used[$num] = 1;
return false;
}
} else {
// Try placing numbers from n down to 1
for ($num = $n; $num >= 1; $num--) {
if ($num == 1) {
if ($used[1] == 0) {
$sequence[$current_pos] = 1;
$used[1] = 1;
$success = $this->backtrack($current_pos + 1, $sequence, $used, $reserved, $n);
if ($success) {
return true;
}
$sequence[$current_pos] = 0;
$used[1] = 0;
}
} else {
if ($used[$num] == 0) {
$next_pos = $current_pos + $num;
if ($next_pos < $length && $sequence[$next_pos] == 0 && $reserved[$next_pos] === null) {
$sequence[$current_pos] = $num;
$used[$num] = 1;
$reserved[$next_pos] = $num;
$success = $this->backtrack($current_pos + 1, $sequence, $used, $reserved, $n);
if ($success) {
return true;
}
$sequence[$current_pos] = 0;
$used[$num] = 0;
$reserved[$next_pos] = null;
}
}
}
}
return false;
}
}
}