-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.ts
41 lines (41 loc) · 1021 Bytes
/
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
40
41
function pushDominoes(dominoes: string): string {
const n = dominoes.length;
const map = {
L: -1,
R: 1,
'.': 0,
};
let ans = new Array(n).fill(0);
let visited = new Array(n).fill(0);
let queue = [];
let depth = 1;
for (let i = 0; i < n; i++) {
let cur = map[dominoes.charAt(i)];
if (cur) {
queue.push(i);
visited[i] = depth;
ans[i] = cur;
}
}
while (queue.length) {
depth++;
let nextLevel = [];
for (let i of queue) {
const dx = ans[i];
let x = i + dx;
if (x >= 0 && x < n && [0, depth].includes(visited[x])) {
ans[x] += dx;
visited[x] = depth;
nextLevel.push(x);
}
}
queue = nextLevel;
}
return ans
.map(d => {
if (!d) return '.';
else if (d < 0) return 'L';
else return 'R';
})
.join('');
}