Skip to content

Commit b98de6c

Browse files
add 1718
1 parent 7b96963 commit b98de6c

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1718|[Construct the Lexicographically Largest Valid Sequence](https://leetcode.com/problems/construct-the-lexicographically-largest-valid-sequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1718.java) ||Medium|Backtracking, Recursion|
1112
|1716|[Calculate Money in Leetcode Bank](https://leetcode.com/problems/calculate-money-in-leetcode-bank/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1716.java) ||Easy|Math, Greedy|
1213
|1711|[Count Good Meals](https://leetcode.com/problems/count-good-meals/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1711.java) ||Medium|Array, HashTable, Two Pointers|
1314
|1710|[Maximum Units on a Truck](https://leetcode.com/problems/maximum-units-on-a-truck/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1710.java) ||Easy|Greedy, Sort|
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1718 {
4+
public static class Solution1 {
5+
public int[] constructDistancedSequence(int n) {
6+
int[] result = new int[n * 2 - 1];
7+
boolean[] visited = new boolean[n + 1];
8+
backtracking(0, result, visited, n);
9+
return result;
10+
}
11+
12+
private boolean backtracking(int index, int[] result, boolean[] visited, int n) {
13+
if (index == result.length) {
14+
return true;
15+
}
16+
if (result[index] != 0) {
17+
return backtracking(index + 1, result, visited, n);
18+
} else {
19+
for (int i = n; i > 0; i--) {
20+
if (visited[i]) {
21+
continue;
22+
}
23+
visited[i] = true;
24+
result[index] = i;
25+
if (i == 1) {
26+
if (backtracking(index + 1, result, visited, n)) {
27+
return true;
28+
}
29+
} else if (index + i < result.length && result[index + i] == 0) {
30+
result[i + index] = i;
31+
if (backtracking(index + 1, result, visited, n)) {
32+
return true;
33+
}
34+
result[index + i] = 0;
35+
}
36+
result[index] = 0;
37+
visited[i] = false;
38+
}
39+
}
40+
return false;
41+
}
42+
}
43+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1718;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _1718Test {
10+
private static _1718.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1718.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertArrayEquals(new int[]{3, 1, 2, 3, 2}, solution1.constructDistancedSequence(3));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)