LeetCode 1503: Last Moment Before All Ants Fall Out of a Plank
Problem Description
Explanation:
- We can find the time when the last ant(s) fall out of the plank by finding the maximum time taken for an ant to reach either end of the plank.
- Ants moving in opposite directions will pass through each other without any collision or time delay.
- We can calculate the time taken for each ant to reach either end of the plank and take the maximum of those times as the answer.
Solutions
class Solution {
public int getLastMoment(int n, int[] left, int[] right) {
int maxTime = 0;
for (int ant : left) {
maxTime = Math.max(maxTime, ant);
}
for (int ant : right) {
maxTime = Math.max(maxTime, n - ant);
}
return maxTime;
}
}
Loading editor...