Skip to content

Commit 484ae46

Browse files
committed
feat: add solutions to lc problems: No.1718,1720
* No.1718.Construct the Lexicographically Largest Valid Sequence * No.1720.Decode XORed Array
1 parent 241c3f1 commit 484ae46

File tree

12 files changed

+600
-29
lines changed

12 files changed

+600
-29
lines changed

solution/1700-1799/1718.Construct the Lexicographically Largest Valid Sequence/README.md

Lines changed: 172 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,193 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50+
DFS 回溯。
51+
5052
<!-- tabs:start -->
5153

5254
### **Python3**
5355

5456
<!-- 这里可写当前语言的特殊实现逻辑 -->
5557

5658
```python
57-
59+
class Solution:
60+
def constructDistancedSequence(self, n: int) -> List[int]:
61+
def dfs(u):
62+
if u == n * 2:
63+
return True
64+
if path[u]:
65+
return dfs(u + 1)
66+
for i in range(n, 1, -1):
67+
if cnt[i] and u + i < n * 2 and path[u + i] == 0:
68+
cnt[i] = 0
69+
path[u] = path[u + i] = i
70+
if dfs(u + 1):
71+
return True
72+
path[u] = path[u + i] = 0
73+
cnt[i] = 2
74+
if cnt[1]:
75+
cnt[1] = 0
76+
path[u] = 1
77+
if dfs(u + 1):
78+
return True
79+
path[u] = 0
80+
cnt[1] = 1
81+
return False
82+
83+
path = [0] * (n * 2)
84+
cnt = [2] * (n * 2)
85+
cnt[1] = 1
86+
dfs(1)
87+
return path[1:]
5888
```
5989

6090
### **Java**
6191

6292
<!-- 这里可写当前语言的特殊实现逻辑 -->
6393

6494
```java
95+
class Solution {
96+
private int[] path;
97+
private int[] cnt;
98+
private int n;
99+
100+
public int[] constructDistancedSequence(int n) {
101+
this.n = n;
102+
path = new int[n * 2];
103+
cnt = new int[n * 2];
104+
Arrays.fill(cnt, 2);
105+
cnt[1] = 1;
106+
dfs(1);
107+
int[] ans = new int[n * 2 - 1];
108+
for (int i = 0; i < ans.length; ++i) {
109+
ans[i] = path[i + 1];
110+
}
111+
return ans;
112+
}
113+
114+
private boolean dfs(int u) {
115+
if (u == n * 2) {
116+
return true;
117+
}
118+
if (path[u] > 0) {
119+
return dfs(u + 1);
120+
}
121+
for (int i = n; i > 1; --i) {
122+
if (cnt[i] > 0 && u + i < n * 2 && path[u + i] == 0) {
123+
cnt[i] = 0;
124+
path[u] = i;
125+
path[u + i] = i;
126+
if (dfs(u + 1)) {
127+
return true;
128+
}
129+
cnt[i] = 2;
130+
path[u] = 0;
131+
path[u + i] = 0;
132+
}
133+
}
134+
if (cnt[1] > 0) {
135+
path[u] = 1;
136+
cnt[1] = 0;
137+
if (dfs(u + 1)) {
138+
return true;
139+
}
140+
cnt[1] = 1;
141+
path[u] = 0;
142+
}
143+
return false;
144+
}
145+
}
146+
```
147+
148+
### **C++**
149+
150+
```cpp
151+
class Solution {
152+
public:
153+
int n;
154+
vector<int> cnt, path;
155+
156+
vector<int> constructDistancedSequence(int _n) {
157+
n = _n;
158+
cnt.resize(n * 2, 2);
159+
path.resize(n * 2);
160+
cnt[1] = 1;
161+
dfs(1);
162+
vector<int> ans;
163+
for (int i = 1; i < n * 2; ++i) ans.push_back(path[i]);
164+
return ans;
165+
}
166+
167+
bool dfs(int u) {
168+
if (u == n * 2) return 1;
169+
if (path[u]) return dfs(u + 1);
170+
for (int i = n; i > 1; --i)
171+
{
172+
if (cnt[i] && u + i < n * 2 && !path[u + i])
173+
{
174+
path[u] = path[u + i] = i;
175+
cnt[i] = 0;
176+
if (dfs(u + 1)) return 1;
177+
cnt[i] = 2;
178+
path[u] = path[u + i] = 0;
179+
}
180+
}
181+
if (cnt[1])
182+
{
183+
path[u] = 1;
184+
cnt[1] = 0;
185+
if (dfs(u + 1)) return 1;
186+
cnt[1] = 1;
187+
path[u] = 0;
188+
}
189+
return 0;
190+
}
191+
};
192+
```
65193
194+
### **Go**
195+
196+
```go
197+
func constructDistancedSequence(n int) []int {
198+
path := make([]int, n*2)
199+
cnt := make([]int, n*2)
200+
for i := range cnt {
201+
cnt[i] = 2
202+
}
203+
cnt[1] = 1
204+
var dfs func(u int) bool
205+
dfs = func(u int) bool {
206+
if u == n*2 {
207+
return true
208+
}
209+
if path[u] > 0 {
210+
return dfs(u + 1)
211+
}
212+
for i := n; i > 1; i-- {
213+
if cnt[i] > 0 && u+i < n*2 && path[u+i] == 0 {
214+
cnt[i] = 0
215+
path[u], path[u+i] = i, i
216+
if dfs(u + 1) {
217+
return true
218+
}
219+
cnt[i] = 2
220+
path[u], path[u+i] = 0, 0
221+
}
222+
}
223+
if cnt[1] > 0 {
224+
cnt[1] = 0
225+
path[u] = 1
226+
if dfs(u + 1) {
227+
return true
228+
}
229+
cnt[1] = 1
230+
path[u] = 0
231+
}
232+
return false
233+
}
234+
dfs(1)
235+
return path[1:]
236+
}
66237
```
67238

68239
### **...**

solution/1700-1799/1718.Construct the Lexicographically Largest Valid Sequence/README_EN.md

Lines changed: 172 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,189 @@
4343

4444
## Solutions
4545

46+
DFS.
47+
4648
<!-- tabs:start -->
4749

4850
### **Python3**
4951

5052
```python
51-
53+
class Solution:
54+
def constructDistancedSequence(self, n: int) -> List[int]:
55+
def dfs(u):
56+
if u == n * 2:
57+
return True
58+
if path[u]:
59+
return dfs(u + 1)
60+
for i in range(n, 1, -1):
61+
if cnt[i] and u + i < n * 2 and path[u + i] == 0:
62+
cnt[i] = 0
63+
path[u] = path[u + i] = i
64+
if dfs(u + 1):
65+
return True
66+
path[u] = path[u + i] = 0
67+
cnt[i] = 2
68+
if cnt[1]:
69+
cnt[1] = 0
70+
path[u] = 1
71+
if dfs(u + 1):
72+
return True
73+
path[u] = 0
74+
cnt[1] = 1
75+
return False
76+
77+
path = [0] * (n * 2)
78+
cnt = [2] * (n * 2)
79+
cnt[1] = 1
80+
dfs(1)
81+
return path[1:]
5282
```
5383

5484
### **Java**
5585

5686
```java
87+
class Solution {
88+
private int[] path;
89+
private int[] cnt;
90+
private int n;
91+
92+
public int[] constructDistancedSequence(int n) {
93+
this.n = n;
94+
path = new int[n * 2];
95+
cnt = new int[n * 2];
96+
Arrays.fill(cnt, 2);
97+
cnt[1] = 1;
98+
dfs(1);
99+
int[] ans = new int[n * 2 - 1];
100+
for (int i = 0; i < ans.length; ++i) {
101+
ans[i] = path[i + 1];
102+
}
103+
return ans;
104+
}
105+
106+
private boolean dfs(int u) {
107+
if (u == n * 2) {
108+
return true;
109+
}
110+
if (path[u] > 0) {
111+
return dfs(u + 1);
112+
}
113+
for (int i = n; i > 1; --i) {
114+
if (cnt[i] > 0 && u + i < n * 2 && path[u + i] == 0) {
115+
cnt[i] = 0;
116+
path[u] = i;
117+
path[u + i] = i;
118+
if (dfs(u + 1)) {
119+
return true;
120+
}
121+
cnt[i] = 2;
122+
path[u] = 0;
123+
path[u + i] = 0;
124+
}
125+
}
126+
if (cnt[1] > 0) {
127+
path[u] = 1;
128+
cnt[1] = 0;
129+
if (dfs(u + 1)) {
130+
return true;
131+
}
132+
cnt[1] = 1;
133+
path[u] = 0;
134+
}
135+
return false;
136+
}
137+
}
138+
```
139+
140+
### **C++**
141+
142+
```cpp
143+
class Solution {
144+
public:
145+
int n;
146+
vector<int> cnt, path;
147+
148+
vector<int> constructDistancedSequence(int _n) {
149+
n = _n;
150+
cnt.resize(n * 2, 2);
151+
path.resize(n * 2);
152+
cnt[1] = 1;
153+
dfs(1);
154+
vector<int> ans;
155+
for (int i = 1; i < n * 2; ++i) ans.push_back(path[i]);
156+
return ans;
157+
}
158+
159+
bool dfs(int u) {
160+
if (u == n * 2) return 1;
161+
if (path[u]) return dfs(u + 1);
162+
for (int i = n; i > 1; --i)
163+
{
164+
if (cnt[i] && u + i < n * 2 && !path[u + i])
165+
{
166+
path[u] = path[u + i] = i;
167+
cnt[i] = 0;
168+
if (dfs(u + 1)) return 1;
169+
cnt[i] = 2;
170+
path[u] = path[u + i] = 0;
171+
}
172+
}
173+
if (cnt[1])
174+
{
175+
path[u] = 1;
176+
cnt[1] = 0;
177+
if (dfs(u + 1)) return 1;
178+
cnt[1] = 1;
179+
path[u] = 0;
180+
}
181+
return 0;
182+
}
183+
};
184+
```
57185
186+
### **Go**
187+
188+
```go
189+
func constructDistancedSequence(n int) []int {
190+
path := make([]int, n*2)
191+
cnt := make([]int, n*2)
192+
for i := range cnt {
193+
cnt[i] = 2
194+
}
195+
cnt[1] = 1
196+
var dfs func(u int) bool
197+
dfs = func(u int) bool {
198+
if u == n*2 {
199+
return true
200+
}
201+
if path[u] > 0 {
202+
return dfs(u + 1)
203+
}
204+
for i := n; i > 1; i-- {
205+
if cnt[i] > 0 && u+i < n*2 && path[u+i] == 0 {
206+
cnt[i] = 0
207+
path[u], path[u+i] = i, i
208+
if dfs(u + 1) {
209+
return true
210+
}
211+
cnt[i] = 2
212+
path[u], path[u+i] = 0, 0
213+
}
214+
}
215+
if cnt[1] > 0 {
216+
cnt[1] = 0
217+
path[u] = 1
218+
if dfs(u + 1) {
219+
return true
220+
}
221+
cnt[1] = 1
222+
path[u] = 0
223+
}
224+
return false
225+
}
226+
dfs(1)
227+
return path[1:]
228+
}
58229
```
59230

60231
### **...**

0 commit comments

Comments
 (0)