{x}
blog image

Last Moment Before All Ants Fall Out of a Plank

We have a wooden plank of the length n units. Some ants are walking on the plank, each ant moves with a speed of 1 unit per second. Some of the ants move to the left, the other move to the right.

When two ants moving in two different 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 a time t, it falls out of the plank immediately.

Given an integer n and two integer arrays left and right, the positions of the ants moving to the left and the right, return the moment when the last ant(s) fall out of the plank.

 

Example 1:

Input: n = 4, left = [4,3], right = [0,1]
Output: 4
Explanation: 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).

Example 2:

Input: n = 7, left = [], right = [0,1,2,3,4,5,6,7]
Output: 7
Explanation: All ants are going to the right, the ant at index 0 needs 7 seconds to fall.

Example 3:

Input: n = 7, left = [0,1,2,3,4,5,6,7], right = []
Output: 7
Explanation: All ants are going to the left, the ant at index 7 needs 7 seconds to fall.

 

Constraints:

  • 1 <= n <= 104
  • 0 <= left.length <= n + 1
  • 0 <= left[i] <= n
  • 0 <= right.length <= n + 1
  • 0 <= right[i] <= n
  • 1 <= left.length + right.length <= n + 1
  • All values of left and right are unique, and each value can appear only in one of the two arrays.

Solution Explanation for 1503. Last Moment Before All Ants Fall Out of a Plank

This problem deals with ants moving on a plank. The crucial observation is that when two ants collide, they effectively swap directions. This means we don't need to simulate the collisions; we can treat each ant as moving independently to the edge of the plank.

Approach

The solution focuses on finding the maximum time it takes for any ant to fall off the plank. For ants moving left, the time is simply their starting position. For ants moving right, the time is the distance to the right edge of the plank (n - position). The algorithm iterates through both left and right arrays, finding the maximum of these times.

Time Complexity Analysis

The algorithm iterates through the left and right arrays once each. The lengths of these arrays are at most O(n), where n is the length of the plank. Therefore, the time complexity is O(n).

Space Complexity Analysis

The algorithm uses a constant amount of extra space to store the maximum time. The space complexity is O(1).

Code Implementation in Multiple Languages

The code below demonstrates the solution in several programming languages. The core logic remains the same across all implementations.

Python3

class Solution:
    def getLastMoment(self, n: int, left: List[int], right: List[int]) -> int:
        ans = 0
        for x in left:
            ans = max(ans, x)
        for x in right:
            ans = max(ans, n - x)
        return ans

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;
    }
}

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;
    }
};

Go

func getLastMoment(n int, left []int, right []int) int {
	ans := 0
	for _, x := range left {
		ans = max(ans, x)
	}
	for _, x := range right {
		ans = max(ans, n-x)
	}
	return ans
}
 
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

JavaScript

/**
 * @param {number} n
 * @param {number[]} left
 * @param {number[]} right
 * @return {number}
 */
var getLastMoment = function(n, left, right) {
    let ans = 0;
    for (let x of left) {
        ans = Math.max(ans, x);
    }
    for (let x of right) {
        ans = Math.max(ans, n - x);
    }
    return ans;
};

TypeScript

function getLastMoment(n: number, left: number[], right: number[]): number {
    let ans = 0;
    for (const x of left) {
        ans = Math.max(ans, x);
    }
    for (const x of right) {
        ans = Math.max(ans, n - x);
    }
    return ans;
};

These code snippets all efficiently solve the problem with linear time and constant space complexity. The core idea is to avoid unnecessary collision simulation by focusing on the individual ants' maximum travel times.