Last Moment Before All Ants Fall Out of a Plank — LeetCode 1503 Python Solution
MediumBrainteaserArraySimulation
- Problem
- #1503
- Reading time
- 2 min
- Source
- leetcode.com
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 ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(n), where n is the length of the plank |
| Space | O(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.