-
Notifications
You must be signed in to change notification settings - Fork 7
/
838-Push-Dominoes.js
41 lines (33 loc) · 1.01 KB
/
838-Push-Dominoes.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
// 838. Push Dominoes [Medium]
// https://leetcode.com/problems/push-dominoes/
/**
* @param {string} dominoes
* @return {string}
*/
const pushDominoes = (dominoes) => {
const dominoesArray = dominoes.split('');
const len = dominoesArray.length;
const forces = new Array(len).fill(0);
let force = 0;
for (let i = 0; i < len; i++) {
const currentDomino = dominoesArray[i];
if (currentDomino === 'R') force = len;
else if (currentDomino === 'L') force = 0;
else force = Math.max(force - 1, 0);
forces[i] += force;
}
for (let i = len - 1; i >= 0; i--) {
const currentDomino = dominoesArray[i];
if (currentDomino === 'L') force = len;
else if (currentDomino === 'R') force = 0;
else force = Math.max(force - 1, 0);
forces[i] -= force;
}
return forces
.map((force) => {
if (force < 0) return 'L';
if (force > 0) return 'R';
return '.';
})
.join('');
};