838. Push Dominoes
Push Dominoes
There are n
dominoes in a line, and we place each domino vertically upright. In the beginning, we simultaneously push some of the dominoes either to the left or to the right.
After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right.
When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces.
For the purposes of this question, we will consider that a falling domino expends no additional force to a falling or already fallen domino.
You are given a string dominoes
representing the initial state where:
dominoes[i] = 'L'
, if theith
domino has been pushed to the left,dominoes[i] = 'R'
, if theith
domino has been pushed to the right, anddominoes[i] = '.'
, if theith
domino has not been pushed.
Return a string representing the final state.
Example 1:
Input: dominoes = "RR.L" Output: "RR.L" Explanation: The first domino expends no additional force on the second domino.
Example 2:

Input: dominoes = ".L.R...LR..L.." Output: "LL.RR.LLRRLL.."
Constraints:
n == dominoes.length
1 <= n <= 105
dominoes[i]
is either'L'
,'R'
, or'.'
.
/**
* @param {string} dominoes
* @return {string}
*/
var pushDominoes = function (dominoes) {
const arr = new Array(dominoes.length)
let c = 0
for (let i = 0; i < dominoes.length; i++) {
if (c > 0 && dominoes[i] === '.') {
c--
} else if (dominoes[i] === 'R') {
c = dominoes.length
} else {
c = 0
}
arr[i] = c
}
c = 0
for (let j = dominoes.length - 1; j >= 0; j--) {
if (c > 0 && dominoes[j] === '.') {
c--
} else if (dominoes[j] === 'L') {
c = dominoes.length
} else {
c = 0
}
arr[j] -= c
}
return arr.map(n => n > 0 ? 'R' : n < 0 ? 'L' : '.').join('')
}
I use the variable to c
represent the magnitude of the thrust, and whenever the thrust source ( R
or L
) is encountered, I c
initialize to the number of dominoes, and after the thrust source, whenever the thrust is met .
, the thrust will be propagated, and the thrust will decay, so c
decrement by 1; whenever the opposite is encountered If the thrust source is used, the thrust will be reset to zero.
As for why the thrust source is initialized to the number of dominoes, this design is so that the force is still positive after all the dominoes are transmitted, but the value decreases every time a grid is passed.
In lines 8 to 17, I start by calculating the right thrust from left to right.
Every encounter .
(line 9) and the current right thrust is greater than 0, the right thrust is decreased; every encounter R
(line 11) is given a thrust source with a size equal to the size of the array, and the rest (that is L
) will be the right thrust reset to zero.
Vice versa, lines 19 to 28 calculate the left thrust in reverse, the only difference is that L
it is given to the thrust source when it encounters R
, and it is reset to zero when it encounters . Incorporate into arr
to calculate the net force.
Take sample capital measurement LL.R
as an example :
Right thrust = 0 0 0 4
Left thrust = -4 -4 0 0
------------------
Net thrust = -4 -4 0 4
LL . R
Right is positive, left is negative, and 0 remains upright (line 29). Therefore, an equilibrium state can be obtained LL.R
.
Another test fund .L.R...LR..L..
:
Right Thrust = 0 0 0 14 13 12 11 0 14 13 12 0 0 0
Left Thrust = -13 -14 0 0 -11 -12 -13 -14 0 -12 -13 -14 0 0
------- -------------------------------------------------
net Thrust = -13 -14 0 14 2 0 -2 -14 14 1 -1 -14 0 0
LL . RR . LLRRLL . .
be in balance LL.RR.LLRRLL..
.
method proven.
OR
var pushDominoes = function(dominoes) {
let start=0;
let end=1;
const arr = ("L"+ dominoes +"R").split("");
while(start < arr.length-1){
while(arr[end]=='.')
end++;
if(arr[start] == arr[end])
for(let i= start+1; i<end; i++) {
arr[i]=arr[start];
}
if(arr[start]>arr[end])
for(let i=1; i<=(end-start-1)/2; i++){
arr[start+i] = 'R';
arr[end-i] = 'L';
}
start=end++;
}
return arr.slice(1,arr.length-1).join("");
};
Conclusion
That’s all folks! In this post, we solved LeetCode problem #838. Push Dominoes
I hope you have enjoyed this post. Feel free to share your thoughts on this.
You can find the complete source code on my GitHub repository. If you like what you learn. feel free to fork 🔪 and star ⭐ it.
In this blog, I have tried to collect & present the most important points to consider when improving Data structure and logic, feel free to add, edit, comment, or ask. For more information please reach me here
Happy coding!
Comments
Post a Comment