Skip to content

Commit

Permalink
Merge pull request #224 from godkingjay/solution-problem-1503
Browse files Browse the repository at this point in the history
  • Loading branch information
godkingjay authored Nov 4, 2023
2 parents 1eaf9d0 + d7fcd66 commit ce98174
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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)

![MEDIUM](https://img.shields.io/badge/-MEDIUM-ffa116?style=for-the-badge&logo=LeetCode&logoColor=white)

<div class="xFUwe" data-track-load="description_content">
<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 class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/06/17/ants.jpg" style="width: 450px; height: 610px;">
<pre><strong>Input:</strong> n = 4, left = [4,3], right = [0,1]
<strong>Output:</strong> 4
<strong>Explanation:</strong> In the image above:
-The ant at index 0 is named A and going to the right.
-The ant at index 1 is named B and going to the right.
-The ant at index 3 is named C and going to the left.
-The ant at index 4 is named D and going to the left.
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).
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/06/17/ants2.jpg" style="width: 639px; height: 101px;">
<pre><strong>Input:</strong> n = 7, left = [], right = [0,1,2,3,4,5,6,7]
<strong>Output:</strong> 7
<strong>Explanation:</strong> All ants are going to the right, the ant at index 0 needs 7 seconds to fall.
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/06/17/ants3.jpg" style="width: 639px; height: 100px;">
<pre><strong>Input:</strong> n = 7, left = [0,1,2,3,4,5,6,7], right = []
<strong>Output:</strong> 7
<strong>Explanation:</strong> All ants are going to the left, the ant at index 7 needs 7 seconds to fall.
</pre>

<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= n &lt;= 10<sup>4</sup></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 <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>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public int getLastMoment(int n, int[] left, int[] right) {
Arrays.sort(left);
Arrays.sort(right);
int t1 = 0;
int t2 = 0;
if (left.length > 0)
t1 = left[left.length - 1];
if (right.length > 0)
t2 = n - right[0];
return Math.max(t1, t2);
}
}

0 comments on commit ce98174

Please sign in to comment.