{"id":7028,"date":"2020-07-05T08:48:27","date_gmt":"2020-07-05T15:48:27","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7028"},"modified":"2020-07-08T17:18:44","modified_gmt":"2020-07-09T00:18:44","slug":"leetcode-1503-last-moment-before-all-ants-fall-out-of-a-plank","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1503-last-moment-before-all-ants-fall-out-of-a-plank\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1503. Last Moment Before All Ants Fall Out of a Plank"},"content":{"rendered":"\n<p>We have a wooden&nbsp;plank of the length&nbsp;<code>n<\/code>&nbsp;<strong>units<\/strong>. Some ants are walking on the&nbsp;plank, each ant moves with speed&nbsp;<strong>1 unit per second<\/strong>. Some of the ants move to the&nbsp;<strong>left<\/strong>, the other move to the&nbsp;<strong>right<\/strong>.<\/p>\n\n\n\n<p>When two ants moving in two&nbsp;<strong>different<\/strong>&nbsp;directions meet at some point, they change their directions and continue moving again. Assume changing directions doesn&#8217;t take any additional time.<\/p>\n\n\n\n<p>When an ant reaches&nbsp;<strong>one end<\/strong>&nbsp;of the plank at a time&nbsp;<code>t<\/code>, it falls out of the plank imediately.<\/p>\n\n\n\n<p>Given an integer&nbsp;<code>n<\/code>&nbsp;and two integer arrays&nbsp;<code>left<\/code>&nbsp;and&nbsp;<code>right<\/code>, the positions of the ants moving to the left and the right.&nbsp;Return&nbsp;<em>the&nbsp;moment<\/em>&nbsp;when the last ant(s) fall out of the plank.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/06\/17\/ants.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 4, left = [4,3], right = [0,1]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> In the image above:\n-The ant at index 0 is named A and going to the right.\n-The ant at index 1 is named B and going to the right.\n-The ant at index 3 is named C and going to the left.\n-The ant at index 4 is named D and going to the left.\nNote that the last moment when an ant was on the plank is t = 4 second, after that it falls imediately out of the plank. (i.e. We can say that at t = 4.0000000001, there is no ants on the plank).\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/06\/17\/ants2.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 7, left = [], right = [0,1,2,3,4,5,6,7]\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> All ants are going to the right, the ant at index 0 needs 7 seconds to fall.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/06\/17\/ants3.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 7, left = [0,1,2,3,4,5,6,7], right = []\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> All ants are going to the left, the ant at index 7 needs 7 seconds to fall.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 9, left = [5], right = [4]\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> At t = 1 second, both ants will be at the same intial position but with different direction.\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 6, left = [6], right = [0]\n<strong>Output:<\/strong> 6\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 10^4<\/code><\/li><li><code>0 &lt;= left.length &lt;= n + 1<\/code><\/li><li><code>0 &lt;= left[i] &lt;= n<\/code><\/li><li><code>0 &lt;= right.length &lt;= n + 1<\/code><\/li><li><code>0 &lt;= right[i] &lt;= n<\/code><\/li><li><code>1 &lt;= left.length + right.length &lt;= n + 1<\/code><\/li><li>All values of&nbsp;<code>left<\/code>&nbsp;and&nbsp;<code>right<\/code>&nbsp;are unique, and each value can appear&nbsp;<strong>only in one<\/strong>&nbsp;of the two arrays.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Keep Walking<\/strong><\/h2>\n\n\n\n<p>When two ants A &#8211;&gt; and &lt;&#8211; B meet at some point, they change directions &lt;&#8211; A  B &#8211;&gt;, we can swap the ids of the ants as &lt;&#8211; B A&#8211;&gt;, so it&#8217;s the same as walking individually and passed by. Then we just need to find the max\/min of the left\/right arrays.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/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\nclass Solution {\npublic:\n  int getLastMoment(int n, vector<int>& left, vector<int>& right) {\n    const int t1 = left.empty() ? 0 : *max_element(cbegin(left), cend(left));\n    const int t2 = right.empty() ? 0 : n - *min_element(cbegin(right), cend(right));\n    return max(t1, t2);\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre>\n\/\/ Author: Huahua\nclass Solution {\n  public int getLastMoment(int n, int[] left, int[] right) {\n    int t1 = left.length == 0 ? 0 : Arrays.stream(left).max().getAsInt();\n    int t2 = right.length == 0 ? 0 : n - Arrays.stream(right).min().getAsInt();\n    return Math.max(t1, t2);\n  }\n}\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def getLastMoment(self, n: int, left: List[int], right: List[int]) -> int:\n    t1 = max(left) if left else 0\n    t2 = n - min(right) if right else 0\n    return max(t1, t2)\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>We have a wooden&nbsp;plank of the length&nbsp;n&nbsp;units. Some ants are walking on the&nbsp;plank, each ant moves with speed&nbsp;1 unit per second. Some of the ants&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[31,177],"class_list":["post-7028","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7028","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=7028"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7028\/revisions"}],"predecessor-version":[{"id":7051,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7028\/revisions\/7051"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}