LeetCode 1503. 所有蚂蚁掉下来前的最后一刻

脑筋急转弯

每只蚂蚁都一样,相遇之后,相当于两人互换身份,继续朝原来的方向前进。因此,找出距离朝向端点最远的蚂蚁需要走多久,就是答案。

class Solution {
    public int getLastMoment(int n, int[] left, int[] right) {
        int res = -1;
        for(int i=0; i < left.length; i++)
            res = Math.max(res, left[i]);
        for(int i=0; i < right.length; i++)
            res = Math.max(res, n-right[i]);
        return res;
    }
}
posted @ 2020-07-13 13:06  li修远  阅读(151)  评论(0编辑  收藏  举报