Last Moment Before All Ants Fall Out of a Plank — LeetCode 1503 Python Solution

MediumBrainteaserArraySimulation
Problem
#1503
Reading time
2 min

The problem

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.

Example

Input
n = 4, left = [4,3], right = [0,1]
Output
4
Explanation
In the image above:

Python solution

Python
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

Complexity

MeasureComplexity
TimeO(n), where n is the length of the plank
SpaceO(1) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 1503. Last Moment Before All Ants Fall Out of a Plank?
LeetCode 1503. Last Moment Before All Ants Fall Out of a Plank is rated Medium on LeetCode.
What is the time complexity of LeetCode 1503. Last Moment Before All Ants Fall Out of a Plank?
The Python solution on this page runs in O(n), where n is the length of the plank.
What is the space complexity of LeetCode 1503. Last Moment Before All Ants Fall Out of a Plank?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 1503. Last Moment Before All Ants Fall Out of a Plank cover?
LeetCode 1503. Last Moment Before All Ants Fall Out of a Plank is tagged Brainteaser, Array and Simulation on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview