-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0838-push-dominoes.js
73 lines (62 loc) · 2 KB
/
0838-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
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
/**
* 838. Push Dominoes
* -----------------------
* link: https://leetcode.com/problems/push-dominoes/
*
* description: follow the rules of dominoes falling physics after one sec.
*
* time: O(n^2)
* space: O(n)
*/
/**
* @param {string} dominoes
* @return {string}
*/
var pushDominoes = function (dominoes) {
if (dominoes.length === 1) return dominoes;
let dominoesArr = [...dominoes];
for (let i = 0; i < dominoesArr.length; i++) {
if (dominoesArr[i] !== '.') continue;
let [left, right] = nearestMove(dominoesArr, i);
if (left === -1 && right === -1) continue;
if (left === -1 && dominoesArr[right] === 'L') {
dominoesArr[i] = 'L';
} else if (right === -1 && dominoesArr[left] === 'R') {
dominoesArr[i] = 'R';
} else if (i - left !== right - i) {
if (i - left < right - i && dominoesArr[left] === 'R') {
dominoesArr[i] = 'R';
dominoesArr[right - 1] = dominoesArr[right];
} else if (i - left > right - i && dominoesArr[right] === 'L') {
dominoesArr[i] = 'L';
dominoesArr[left + 1] = dominoesArr[left];
} else if (dominoesArr[left] === dominoesArr[right]) {
dominoesArr[i] = dominoesArr[right];
}
} else if (dominoesArr[left] === dominoesArr[right]) {
dominoesArr[i] = dominoesArr[right];
}
}
return dominoesArr.join('');
};
/**
* @param {string[]} dominoes
* @param {number} index
* @returns {number[]}
*/
var nearestMove = function (dominoes, index) {
let ans = [-1, -1];
for (let i = index - 1; i > -1; i--) {
if (ans[0] === -1 && (dominoes[i] === 'L' || dominoes[i] === 'R')) {
ans[0] = i;
break;
}
}
for (let i = index + 1; i < dominoes.length; i++) {
if (ans[1] === -1 && (dominoes[i] === 'L' || dominoes[i] === 'R')) {
ans[1] = i;
break;
}
}
return ans;
};