MediumNo. 1503
Last Moment Before All Ants Fall Out of a Plank — LeetCode 1503 Python Solution
- Topics
- Brainteaser, Array, Simulation
- 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
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
- Time
- O(n)where n is the length of the plank
- Space · auxiliary
- O(1)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0495 | Teemo Attacking | Easy |
| 0985 | Sum of Even Numbers After Queries | Medium |
| 1389 | Create Target Array in the Given Order | Easy |
| 1409 | Queries on a Permutation With Key | Medium |
| 1535 | Find the Winner of an Array Game | Medium |
| 1560 | Most Visited Sector in a Circular Track | Easy |
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.