描述:有一块长度为 n 的木板上,n+1 个位置标记为 0,1,…,n。给定两个数组 left 和 right,表示蚂蚁的初始位置。left 中的蚂蚁向左移动,right 中的蚂蚁向右移动。所有蚂蚁同时以每秒 1 单位的速度移动。当两只蚂蚁相遇时,它们会立即掉头。
要求:返回所有蚂蚁都从木板上掉下去的最后时刻。
说明:
1≤n≤104。
0≤left.length,right.length≤n+1。
所有蚂蚁位置保证不重叠。
示例:
示例 1:
输入:n = 4, left = [4,3], right = [0,1]输出:4解释:如上图所示:-下标 0 处的蚂蚁命名为 A 并向右移动。-下标 1 处的蚂蚁命名为 B 并向右移动。-下标 3 处的蚂蚁命名为 C 并向左移动。-下标 4 处的蚂蚁命名为 D 并向左移动。请注意,蚂蚁在木板上的最后时刻是 t = 4 秒,之后蚂蚁立即从木板上掉下来。(也就是说在 t = 4.0000000001 时,木板上没有蚂蚁)。
示例 2:
输入:n = 7, left = [], right = [0,1,2,3,4,5,6,7]输出:7解释:所有蚂蚁都向右移动,下标为 0 的蚂蚁需要 7 秒才能从木板上掉落。
class Solution: def getLastMoment(self, n: int, left: List[int], right: List[int]) -> int: ans = 0 for pos in left: ans = max(ans, pos) for pos in right: ans = max(ans, n - pos) return ans