在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ On a plane there are n points with integer coordinates points[i] = [xi, yi]. Your task is to find the minimum time in seconds to visit all points. You can move according to the next rules: In one second always you can either move vertically, horizontally by one unit or diagonally (it means to move one unit vertically and one unit horizontally in one second). Example 1: Input: points = [[1,1],[3,4],[-1,0]] Input: points = [[3,2],[-2,2]] Constraints: points.length == n 平面上有 n 个点,点的位置用整数坐标表示 points[i] = [xi, yi]。请你计算访问所有这些点需要的最小时间(以秒为单位)。 你可以按照下面的规则在平面上移动: 每一秒沿水平或者竖直方向移动一个单位长度,或者跨过对角线(可以看作在一秒内向水平和竖直方向各移动一个单位长度)。 示例 1: 输入:points = [[1,1],[3,4],[-1,0]] 输入:points = [[3,2],[-2,2]] 提示: points.length == n Runtime: 44 ms
Memory Usage: 20.9 MB
1 class Solution { 2 func minTimeToVisitAllPoints(_ points: [[Int]]) -> Int { 3 var ans:Int = 0 4 for i in 1..<points.count 5 { 6 let cur:[Int] = points[i] 7 let prev = points[i - 1] 8 ans += max(abs(cur[0] - prev[0]), abs(cur[1] - prev[1])) 9 } 10 return ans 11 } 12 }
|
请发表评论