Class Solution
java.lang.Object
g1501_1600.s1503_last_moment_before_all_ants_fall_out_of_a_plank.Solution
1503 - Last Moment Before All Ants Fall Out of a Plank.<p>Medium</p>
<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>
<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>
<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>
<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>
<p><strong>Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2020/06/17/ants.jpg" alt="" /></p>
<p><strong>Input:</strong> n = 4, left = [4,3], right = [0,1]</p>
<p><strong>Output:</strong> 4</p>
<p><strong>Explanation:</strong> In the image above:</p>
<p>-The ant at index 0 is named A and going to the right.</p>
<p>-The ant at index 1 is named B and going to the right.</p>
<p>-The ant at index 3 is named C and going to the left.</p>
<p>-The ant at index 4 is named D and going to the left.</p>
<p>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).</p>
<p><strong>Example 2:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2020/06/17/ants2.jpg" alt="" /></p>
<p><strong>Input:</strong> n = 7, left = [], right = [0,1,2,3,4,5,6,7]</p>
<p><strong>Output:</strong> 7</p>
<p><strong>Explanation:</strong> All ants are going to the right, the ant at index 0 needs 7 seconds to fall.</p>
<p><strong>Example 3:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2020/06/17/ants3.jpg" alt="" /></p>
<p><strong>Input:</strong> n = 7, left = [0,1,2,3,4,5,6,7], right = []</p>
<p><strong>Output:</strong> 7</p>
<p><strong>Explanation:</strong> All ants are going to the left, the ant at index 7 needs 7 seconds to fall.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= left.length <= n + 1</code></li>
<li><code>0 <= left[i] <= n</code></li>
<li><code>0 <= right.length <= n + 1</code></li>
<li><code>0 <= right[i] <= n</code></li>
<li><code>1 <= left.length + right.length <= n + 1</code></li>
<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>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
getLastMoment
public int getLastMoment(int n, int[] left, int[] right)
-