Skip to content

Commit 02a84af

Browse files
committed
Runtime: 81 ms (Top 96.77%) | Memory: 56.70 MB (Top 74.19%)
1 parent 81be03a commit 02a84af

File tree

1 file changed

+17
-52
lines changed

1 file changed

+17
-52
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,20 @@
1-
/**
2-
* @param {string} dominoes
3-
* @return {string}
4-
*/
1+
// Runtime: 81 ms (Top 96.77%) | Memory: 56.70 MB (Top 74.19%)
2+
53
var pushDominoes = function(dominoes) {
6-
7-
let len = dominoes.length;
8-
let fall = [];
9-
let force = 0;
10-
let answer = "";
11-
12-
// Traverse from left to right. Focus on the dominoes falling to the right
13-
for (let i = 0; i < len; i++) {
14-
if (dominoes[i] == 'R') {
15-
force = len;
16-
} else if (dominoes[i] == 'L') {
17-
force = 0;
18-
} else {
19-
force = Math.max(force-1,0);
20-
}
21-
fall[i] = force;
4+
let l=0, r=1;
5+
const arr = ("L"+dominoes+"R").split("");
6+
while(l<arr.length-1){
7+
while(arr[r]=='.')
8+
r++;
9+
if(arr[l]==arr[r])
10+
for(let i=l+1; i<r; i++)
11+
arr[i]=arr[l];
12+
if(arr[l]>arr[r])
13+
for(let i=1; i<=(r-l-1)/2; i++){
14+
arr[l+i] = 'R';
15+
arr[r-i] = 'L';
16+
}
17+
l=r++;
2218
}
23-
24-
//console.log('fall array 1: ', fall);
25-
26-
// Traverse from right to left. Focus on the dominoes falling to the left
27-
// Subtract the value from the values above
28-
for (let i = len-1; i >= 0; i--) {
29-
if (dominoes[i] == 'L') {
30-
force = len;
31-
} else if (dominoes[i] == 'R') {
32-
force = 0;
33-
} else {
34-
force = Math.max(force-1,0);
35-
}
36-
fall[i] -= force;
37-
}
38-
39-
//console.log('fall array 2: ', fall);
40-
41-
// Just traverse through the fall[] array and assign the values in the answer string accordingly
42-
for (let i = 0; i < len; i++) {
43-
if (fall[i] < 0)
44-
answer += 'L';
45-
else if (fall[i] > 0 )
46-
answer += 'R';
47-
else
48-
answer += '.';
49-
}
50-
51-
//console.log('answer: ', answer);
52-
53-
return answer;
54-
19+
return arr.slice(1,arr.length-1).join("");
5520
};

0 commit comments

Comments
 (0)