24. Last Moment Before All Ants Fall Out
β GFG solution to the Last Moment Before All Ants Fall Out problem: find when the last ant falls off the plank using optimal physics-based approach. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given a wooden plank of length n
units. Some ants are walking on the plank, some are walking left and some are walking right. Each ant moves with speed 1 unit per second.
When two ants moving in opposite directions meet at some point, they change their directions and continue moving again. Assume changing directions does not take any additional time. When an ant reaches one end of the plank at time t
, it falls out of the plank immediately.
Given an integer n
and two integer arrays left[]
and right[]
, representing the positions of ants moving to the left and right respectively, return the time when the last ant(s) fall out of the plank.
π Examples
Example 1
Input: n = 4, left[] = [2], right[] = [0, 1, 3]
Output: 4
Explanation: As seen in the problem, the last ant falls off the plank at t = 4.
Example 2
Input: n = 4, left[] = [], right[] = [0, 1, 2, 3, 4]
Output: 4
Explanation: All ants are going to the right, the ant at index 0 needs 4 seconds to fall.
Example 3
Input: n = 3, left[] = [0], right[] = [3]
Output: 0
Explanation: The ants will fall off the plank as they are already on the end of the plank.
π Constraints
$1 \le n \le 10^5$
$0 \le \text{left.length, right.length} \le n + 1$
$0 \le \text{left}[i], \text{right}[i] \le n$
$1 \le \text{left.length + right.length} \le n + 1$
All values are unique and each value appears in only one array
β
My Approach
The key insight is that when two ants collide and change directions, we can think of them as passing through each other without changing direction. This is because the system behaves identically - the positions and movements remain the same, only the "identity" of ants changes.
Physics-Based Optimization
Key Insight:
When ants collide, instead of tracking direction changes, we can imagine they pass through each other.
The time for the last ant to fall is simply the maximum time any ant needs to reach either end.
Calculate Time for Each Ant:
For ants moving left: Time = current position (distance to left edge = 0)
For ants moving right: Time = n - current position (distance to right edge = n)
Find Maximum Time:
The answer is the maximum of all individual times.
This represents when the last ant will fall off the plank.
Why This Works:
Collisions don't affect the overall system behavior.
Each ant will eventually fall off at the same time as if there were no collisions.
We only need to find the ant that takes the longest time to reach an edge.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(L + R), where L is the length of left array and R is the length of right array. We iterate through both arrays once to find the maximum values.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for storing the maximum value and loop variables.
π§βπ» Code (C++)
class Solution {
public:
int getLastMoment(int n, vector<int>& left, vector<int>& right) {
int ans = 0;
for (int x : left) ans = max(ans, x);
for (int x : right) ans = max(ans, n - x);
return ans;
}
};
π§βπ» Code (Java)
class Solution {
public int getLastMoment(int n, int left[], int right[]) {
int ans = 0;
for (int x : left) ans = Math.max(ans, x);
for (int x : right) ans = Math.max(ans, n - x);
return ans;
}
}
π Code (Python)
class Solution:
def getLastMoment(self, n, left, right):
ans = 0
for x in left: ans = max(ans, x)
for x in right: ans = max(ans, n - x)
return ans
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated