-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.ts
39 lines (38 loc) · 1.11 KB
/
solution.ts
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
function platesBetweenCandles (s: string, queries: number[][]): number[] {
const prefix = new Array<number>(s.length + 1);
prefix[0] = 0;
const leftNearestCancleIndexs = new Array<number>(s.length);
const rightNearestCandleIndexs = new Array<number>(s.length);
let l = -1;
let c = 0;
for (let i = 0; i < s.length; i++) {
if (s[i] === '|') {
l = i;
} else {
c++;
}
prefix[i + 1] = c;
leftNearestCancleIndexs[i] = l;
}
let r = s.length;
for (let i = s.length - 1; i > -1; i--) {
if (s[i] === '|') {
r = i;
}
rightNearestCandleIndexs[i] = r;
}
return queries.map(([l, r, ]) => {
let res = prefix[r + 1] - prefix[l];
const firstRight = rightNearestCandleIndexs[l];
if (firstRight >= r) {
return 0;
}
res -= (prefix[firstRight] - prefix[l]);
const firstLeft = leftNearestCancleIndexs[r];
if (firstLeft <= l) {
return 0;
}
res -= (prefix[r + 1] - prefix[firstLeft]);
return res;
});
}