-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (58 loc) · 1.63 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import convert from "./Medium/6_ZigzagConversion.js";
var getHint = function (secret, guess) {
let x = 0,
y = 0;
const matrix = new Array(secret.length + 1)
.fill()
.map((_) => new Array(guess.length + 1).fill(false));
for (let i = 1; i < secret.length; i++) {
for (let j = 1; j < guess.length; j++) {
if (guess[i - 1] === secret[j - 1] && i === j && !matrix[i - 1][j]) {
matrix[i][j] = true;
x++;
continue;
} else if (
guess[i - 1] === secret[j - 1] &&
i !== j &&
!matrix[i - 1][j]
) {
y++;
matrix[i][j - 1] = true;
} else {
matrix[i][j - 1] = matrix[i - 1][j];
}
}
}
console.log({ matrix, x, y });
};
var longestValidParentheses = function (s) {
if (s.length === 0) return 0;
const pairs = { "(": ")" };
let numResult = 0;
const unCloseStack = [];
unCloseStack.push({ v: s[0], i: 0 });
for (let i = 1; i < s.length; i++) {
if (pairs[unCloseStack[unCloseStack.length - 1]?.v] === s[i]) {
unCloseStack.pop();
numResult += 2;
} else {
unCloseStack.push({ v: s[i], i: i });
}
}
if (unCloseStack.length > 0) {
const newUnclose = unCloseStack.map((v) => v.i);
newUnclose.push(0);
newUnclose.push(s.length - 1);
newUnclose.sort((a, b) => a - b);
const lengthResult = [];
for (let i = 1; i < newUnclose.length; i++) {
const sub = newUnclose[i] - newUnclose[i - 1];
if (sub >= 2) {
lengthResult.push(sub);
}
}
numResult = Math.max(...lengthResult);
}
return numResult;
};
console.log(longestValidParentheses(")()()()("));