|
| 1 | +# Problem 1503 ([Last Moment Before All Ants Fall Out of a Plank](https://leetcode.com/problems/last-moment-before-all-ants-fall-out-of-a-plank/) | Array, Brainteaser, Simulation) |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +<div class="xFUwe" data-track-load="description_content"> |
| 6 | +<p>We have a wooden plank of the length <code>n</code> <strong>units</strong>. Some ants are walking on the plank, each ant moves with a speed of <strong>1 unit per second</strong>. Some of the ants move to the <strong>left</strong>, the other move to the <strong>right</strong>.</p> |
| 7 | + |
| 8 | +<p>When two ants moving in two <strong>different</strong> directions meet at some point, they change their directions and continue moving again. Assume changing directions does not take any additional time.</p> |
| 9 | + |
| 10 | +<p>When an ant reaches <strong>one end</strong> of the plank at a time <code>t</code>, it falls out of the plank immediately.</p> |
| 11 | + |
| 12 | +<p>Given an integer <code>n</code> and two integer arrays <code>left</code> and <code>right</code>, the positions of the ants moving to the left and the right, return <em>the moment when the last ant(s) fall out of the plank</em>.</p> |
| 13 | + |
| 14 | +<p><strong class="example">Example 1:</strong></p> |
| 15 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/06/17/ants.jpg" style="width: 450px; height: 610px;"> |
| 16 | +<pre><strong>Input:</strong> n = 4, left = [4,3], right = [0,1] |
| 17 | +<strong>Output:</strong> 4 |
| 18 | +<strong>Explanation:</strong> In the image above: |
| 19 | +-The ant at index 0 is named A and going to the right. |
| 20 | +-The ant at index 1 is named B and going to the right. |
| 21 | +-The ant at index 3 is named C and going to the left. |
| 22 | +-The ant at index 4 is named D and going to the left. |
| 23 | +The last moment when an ant was on the plank is t = 4 seconds. After that, it falls immediately out of the plank. (i.e., We can say that at t = 4.0000000001, there are no ants on the plank). |
| 24 | +</pre> |
| 25 | + |
| 26 | +<p><strong class="example">Example 2:</strong></p> |
| 27 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/06/17/ants2.jpg" style="width: 639px; height: 101px;"> |
| 28 | +<pre><strong>Input:</strong> n = 7, left = [], right = [0,1,2,3,4,5,6,7] |
| 29 | +<strong>Output:</strong> 7 |
| 30 | +<strong>Explanation:</strong> All ants are going to the right, the ant at index 0 needs 7 seconds to fall. |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p><strong class="example">Example 3:</strong></p> |
| 34 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/06/17/ants3.jpg" style="width: 639px; height: 100px;"> |
| 35 | +<pre><strong>Input:</strong> n = 7, left = [0,1,2,3,4,5,6,7], right = [] |
| 36 | +<strong>Output:</strong> 7 |
| 37 | +<strong>Explanation:</strong> All ants are going to the left, the ant at index 7 needs 7 seconds to fall. |
| 38 | +</pre> |
| 39 | + |
| 40 | +<p><strong>Constraints:</strong></p> |
| 41 | + |
| 42 | +<ul> |
| 43 | + <li><code>1 <= n <= 10<sup>4</sup></code></li> |
| 44 | + <li><code>0 <= left.length <= n + 1</code></li> |
| 45 | + <li><code>0 <= left[i] <= n</code></li> |
| 46 | + <li><code>0 <= right.length <= n + 1</code></li> |
| 47 | + <li><code>0 <= right[i] <= n</code></li> |
| 48 | + <li><code>1 <= left.length + right.length <= n + 1</code></li> |
| 49 | + <li>All values of <code>left</code> and <code>right</code> are unique, and each value can appear <strong>only in one</strong> of the two arrays.</li> |
| 50 | +</ul> |
| 51 | +</div> |
0 commit comments