题目如下:

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

解题思路:优先走斜线,直到满足起点的x坐标等于终点的x坐标或者起点的y坐标等于终点的y坐标;然后走直线,直至到达终点。

代码如下:

class Solution(object):
def minTimeToVisitAllPoints(self, points):
"""
:type points: List[List[int]]
:rtype: int
"""
def calc(x1,y1,x2,y2):
distance = min(abs(x1-x2),abs(y1-y2))
return distance + abs(abs(x1-x2) - distance) + abs(abs(y1-y2) - distance)
res = 0
for i in range(len(points)-1):
res += calc(points[i][0],points[i][1],points[i+1][0],points[i+1][1])
return res

最新文章

  1. Java javassist动态代理
  2. MapReduce编程示例
  3. android学习笔记30——AndroidMainfest.xml
  4. 【转】【Android测试技巧】01. root后adb shell默认不是root用户时,如何将文件放入手机系统中
  5. 从零开始搭建TestCpp工程
  6. git pull 部署问题一揽子问题解决
  7. shell script 入门 笔记
  8. erlang四大behaviour之三-gen_event
  9. .net 企业管理系统快熟搭建框架
  10. Spring事务管理—aop:pointcut expression解析
  11. React 虚拟 DOM 的差异检测机制
  12. WebGL绘制有宽度的线
  13. Python day 05
  14. (转)db2top详解
  15. selective search
  16. 转:vue+canvas如何实现b站萌系登录界面
  17. C# System.Collections.Stack
  18. Java基础加强总结(一)——注解(Annotation)
  19. Spring Boot 揭秘与实战(二) 数据存储篇 - JPA整合
  20. HttpServlet Service方法

热门文章

  1. python 实现 灰色预测 GM(1,1)模型 灰色系统 预测 灰色预测公式推导
  2. JDBC基本操作
  3. Windows编程中各种操作文件的方法
  4. Linux 概念与快捷方式
  5. 面向服务架构之RPC原理与实例
  6. Eclipse中导入Maven Web项目并配置其在Tomcat中运行
  7. sed---流文本操作
  8. POI操作OFFICE转HTML
  9. php和java语法区别
  10. @RequestMapping-限定参数映射