Skip to content

Commit 01c34e5

Browse files
committed
feat(leetcode): add No.1718
1 parent 556dd35 commit 01c34e5

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

1717.Maximum-Score-From-Removing-Substrings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// https://leetcode.com/problems/maximum-score-from-removing-substrings/
22
// algorithms
3-
// Easy (37.47%)
3+
// Medium (37.47%)
44
// Total Accepted: 3,920
55
// Total Submissions: 10,461
66

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// https://leetcode.com/problems/construct-the-lexicographically-largest-valid-sequence/
2+
// algorithms
3+
// Medium (42.24%)
4+
// Total Accepted: 2,795
5+
// Total Submissions: 6,617
6+
7+
8+
class Solution {
9+
10+
private static int[] res;
11+
12+
private static boolean success;
13+
14+
public int[] constructDistancedSequence(int n) {
15+
res = new int[2 * n - 1];
16+
success = false;
17+
18+
recursive(0, n, new int[2 * n - 1], new HashSet<>());
19+
20+
return res;
21+
}
22+
23+
private void recursive(int idx, int n, int[] tmp, Set<Integer> set) {
24+
if (success) {
25+
return;
26+
}
27+
28+
int len = res.length;
29+
if (idx == len) {
30+
for (int i = 0; i < len; i++) {
31+
res[i] = tmp[i];
32+
}
33+
34+
success = true;
35+
return;
36+
}
37+
38+
if (tmp[idx] != 0) {
39+
recursive(idx + 1, n, tmp, set);
40+
return;
41+
}
42+
43+
for (int i = n; i > 0; i--) {
44+
if (set.contains(i)) {
45+
continue;
46+
}
47+
if (i != 1 && (idx + i >= len || tmp[idx + i] != 0)) {
48+
continue;
49+
}
50+
51+
tmp[idx] = i;
52+
if (i != 1) {
53+
tmp[idx + i] = i;
54+
}
55+
set.add(i);
56+
57+
recursive(idx + 1, n, tmp, set);
58+
59+
if (success) {
60+
return;
61+
}
62+
63+
tmp[idx] = 0;
64+
if (i != 1) {
65+
tmp[idx + i] = 0;
66+
}
67+
set.remove(i);
68+
}
69+
70+
}
71+
}

0 commit comments

Comments
 (0)