zoukankan      html  css  js  c++  java
  • [Swift]LeetCode1266. 访问所有点的最小时间 | Minimum Time Visiting All Points

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(let_us_code)
    ➤博主域名:https://www.zengqiang.org
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/11921953.html
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    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).
    You have to visit the points in the same order as they appear in the array.

    Example 1:

    Input: points = [[1,1],[3,4],[-1,0]]
    Output: 7
    Explanation: One optimal path is [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0]
    Time from [1,1] to [3,4] = 3 seconds
    Time from [3,4] to [-1,0] = 4 seconds
    Total time = 7 seconds
    Example 2:

    Input: points = [[3,2],[-2,2]]
    Output: 5

    Constraints:

    points.length == n
    1 <= n <= 100
    points[i].length == 2
    -1000 <= points[i][0], points[i][1] <= 1000


    平面上有 n 个点,点的位置用整数坐标表示 points[i] = [xi, yi]。请你计算访问所有这些点需要的最小时间(以秒为单位)。

    你可以按照下面的规则在平面上移动:

    每一秒沿水平或者竖直方向移动一个单位长度,或者跨过对角线(可以看作在一秒内向水平和竖直方向各移动一个单位长度)。
    必须按照数组中出现的顺序来访问这些点。

    示例 1:

    输入:points = [[1,1],[3,4],[-1,0]]
    输出:7
    解释:一条最佳的访问路径是: [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0]
    从 [1,1] 到 [3,4] 需要 3 秒
    从 [3,4] 到 [-1,0] 需要 4 秒
    一共需要 7 秒
    示例 2:

    输入:points = [[3,2],[-2,2]]
    输出:5

    提示:

    points.length == n
    1 <= n <= 100
    points[i].length == 2
    -1000 <= points[i][0], points[i][1] <= 1000


    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 }
  • 相关阅读:
    安装node和npm
    安装git
    常用软件
    vscode常用插件
    git生成ssh key
    04.接口初始化规则与类加载器准备阶段和初始化阶段的意义
    03.编译期常量与运行期常量的区别与数组创建本质分析
    02.常量的本质含义与反编译及助记符
    01.类加载,连接与初始化过程
    HTTP 状态码大全
  • 原文地址:https://www.cnblogs.com/strengthen/p/11921953.html
Copyright © 2011-2022 走看看