{"id":5152,"date":"2019-05-06T22:22:16","date_gmt":"2019-05-07T05:22:16","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5152"},"modified":"2019-05-06T22:40:45","modified_gmt":"2019-05-07T05:40:45","slug":"leetcode-838-push-dominoes","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-838-push-dominoes\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 838. Push Dominoes"},"content":{"rendered":"\n<p>here are&nbsp;<code>N<\/code>&nbsp;dominoes in a line, and we place each domino vertically upright.<\/p>\n\n\n\n<p>In the beginning, we simultaneously push&nbsp;some of the dominoes either to the left or to the right.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/s3-lc-upload.s3.amazonaws.com\/uploads\/2018\/05\/18\/domino.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>After each second, each domino that is falling to the left pushes the adjacent domino on the left.<\/p>\n\n\n\n<p>Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right.<\/p>\n\n\n\n<p>When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces.<\/p>\n\n\n\n<p>For the purposes of this question, we will consider that a falling domino&nbsp;expends no additional force to a falling or already fallen domino.<\/p>\n\n\n\n<p>Given a string &#8220;S&#8221; representing the initial state.&nbsp;<code>S[i] = 'L'<\/code>, if the i-th domino has been pushed to the left;&nbsp;<code>S[i] = 'R'<\/code>, if the i-th domino has been pushed to the right;&nbsp;<code>S[i] = '.'<\/code>,&nbsp;if the&nbsp;<code>i<\/code>-th domino has not been pushed.<\/p>\n\n\n\n<p>Return a string representing the final state.&nbsp;<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input: <\/strong>\".L.R...LR..L..\"\n<strong>Output: <\/strong>\"LL.RR.LLRRLL..\"\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input: <\/strong>\"RR.L\"\n<strong>Output: <\/strong>\"RR.L\"\n<strong>Explanation: <\/strong>The first domino expends no additional force on the second domino.\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>0 &lt;= N&nbsp;&lt;= 10^5<\/code><\/li><li>String&nbsp;<code>dominoes<\/code>&nbsp;contains only&nbsp;<code>'L<\/code>&#8216;,&nbsp;<code>'R'<\/code>&nbsp;and&nbsp;<code>'.'<\/code><\/li><\/ol>\n\n\n\n<p><strong>Solution: Simulation<\/strong><\/p>\n\n\n\n<p>Simulate the push process, record the steps from L and R for each domino.<br>steps(L) ==  steps(R) => &#8220;.&#8221;<br>steps(L) &lt; steps(R) => &#8220;L&#8221;<br>steps(L) > steps(R) => &#8220;R&#8221;<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, running time: 24 ms, 15.2 MB\nclass Solution {\npublic:\n  string pushDominoes(string D) {\n    const int n = static_cast<int>(D.size());\n    vector<int> L(n, INT_MAX), R(n, INT_MAX);    \n    \n    for (int i = 0; i < n; ++i)\n      if (D[i] == 'L') {\n        L[i] = 0;\n        for (int j = i - 1; j >= 0 && D[j] == '.'; --j)\n          L[j] = L[j + 1] + 1;\n      } else if (D[i] == 'R') {        \n        R[i] = 0;\n        for (int j = i + 1; j < n &#038;&#038; D[j] == '.'; ++j)\n          R[j] = R[j - 1] + 1;\n      }\n    \n    for (int i = 0; i < n; ++i)\n      if (L[i] < R[i]) D[i] = 'L';\n      else if (L[i] > R[i]) D[i] = 'R';\n    \n    return D;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>here are&nbsp;N&nbsp;dominoes in a line, and we place each domino vertically upright. In the beginning, we simultaneously push&nbsp;some of the dominoes either to the left&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[34,177,179],"class_list":["post-5152","post","type-post","status-publish","format-standard","hentry","category-searching","tag-bfs","tag-medium","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5152"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5152\/revisions"}],"predecessor-version":[{"id":5158,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5152\/revisions\/5158"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}