1.原题:

https://leetcode.com/problems/minimum-time-visiting-all-points/

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 arra

翻译:给定一个平面,有n个点,坐标表示为 [xi,yi],你需要求出一个最小的访问时间,你的访问方式必须是以顺序去访问所有的点。每一秒钟你可以水平或者垂直移动一步或者对角移动一步。

此图就是原题的一个示例:

Input : points = [[1,1],[3,4],[-1,0]]

output :7

因为 :[1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0]

2.解题思路:

这种简单的最小路径的算法题,思路有很多,最简单的就是贪心算法,因为最无脑。

我们都知道,对角的一步比水平或者垂直移动的一步都要远,所以我们要尽量先走对角,然后实在没有办法的时候再普通的走一步。

那么算法就很明了了:

class Solution {
public:
int minTimeToVisitAllPoints(vector<vector<int>>& points) {  
int ans = 0;   //距离的绝对值
for(int i = 1; i < points.size(); i++) {   
ans += max(abs(points[i][1] - points[i - 1][1]), abs(points[i][0] - points[i - 1][0]));    //求出发点和目的点的xy值的差,比如例子里,x为3-1=2,y为4-1=3。这两个值相同的部分(2) 就是对角的步数,多出来的(1)就是水平或者垂直的移动,因此我们取两者的最大值3,是本次的距离。
}
return ans;
}
};

sub:这里要注意就是 “points.size()” 这个是vector的一个成员函数 .size()其返回的是unsighed int,所以注意不要越界。

leetcode菜鸡斗智斗勇系列(7)--- 用最小的时间访问所有的节点的更多相关文章

  1. leetcode菜鸡斗智斗勇系列(4)--- 单一数字的乘积和总合的减法

    1.原题: https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/ Given an i ...

  2. leetcode菜鸡斗智斗勇系列(3)--- Jewels and Stones珠宝和钻石

    1.原题: https://leetcode.com/problems/jewels-and-stones/ You're given strings J representing the types ...

  3. leetcode菜鸡斗智斗勇系列(2)--- 把一个ipv4地址转换成一串数字

    1.原题: https://leetcode.com/problems/defanging-an-ip-address/ 这道题本身很简单, Given a valid (IPv4) IP addre ...

  4. leetcode菜鸡斗智斗勇系列(1)---把一个链表中的二进制数字转换为一个整型数(int)

    Convert Binary Number in a Linked List to Integer这道题在leetcode上面算作是“easy”,然而小生我还是不会做,于是根据大佬的回答来整理一下思路 ...

  5. leetcode菜鸡斗智斗勇系列(10)--- Decrypt String from Alphabet to Integer Mapping

    1.原题: https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/submissions/ Giv ...

  6. leetcode菜鸡斗智斗勇系列(9)--- Range Sum of BST

    1.原题: https://leetcode.com/problems/range-sum-of-bst/ Given the root node of a binary search tree, r ...

  7. leetcode菜鸡斗智斗勇系列(8)--- Find N Unique Integers Sum up to Zero

    1.原题: https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/ Given an integer n, retur ...

  8. leetcode菜鸡斗智斗勇系列(6)--- 检查一个string里面有几个对称的字段

    1.原题: https://leetcode.com/problems/split-a-string-in-balanced-strings/ Split a String in Balanced S ...

  9. leetcode菜鸡斗智斗勇系列(5)--- 寻找拥有偶数数位的数字

    1.原题: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/ Given an array nums of ...

随机推荐

  1. Bugku-CTF之细心 (想办法变成admin)

    Day30     细心

  2. 【应急响应】Linux安全加固

    一.补丁管理 1.查看系统信息 uname -a 2.配置yun源 CentosOS 可以直接升级 RHEL系列可以配置使用CentosOS源 3.升级软件包 yum –y update 二.安全工具 ...

  3. Python socket day4

    TCP(较UDP麻烦但安全) 服务器和客户端区分的很明白 TCP客户端比起UDP多个连接服务器 TCP服务端 socket创建一个套接字 一定要绑定IP和端口,就跟110一样,是固定的让人随时能知道 ...

  4. tableSizeFor()函数在java8和Java13的差别

    java8 static final int tableSizeFor(int cap) { int n = cap - 1; n |= n >>> 1; n |= n >&g ...

  5. go基础_切片

    切片创建方式 1.通过数组创建 2.通过内置函数make创建 切片允许的操作 1.追加元素 2.通过内置函数make创建 package main import "fmt" fun ...

  6. 排序:ORDER BY

    1.按照字段值进行排序: 语法:order by 字段 升序|降序 (asc|desc) 默认情况下为“升序” asc.asc=ascending 升 desc=descending 降 2.允许多字 ...

  7. 每天进步一点点------Error: Can't place pins assigned to pin location Pin_K22 (IOPAD_X41_Y19_N14)

    在QII中的Assignments----Device----Device and pin option-----(选项卡)Dual purpose pin将nCE0 的设置改为: use as re ...

  8. LINUX 怎么实现root和普通用户的切换及怎么更改root密码

    在linux系统中执行什么命令后可以使$变为#?     先说下$和#在linux系统终端(命令行)中通常代表的什么:$打头的表示这不是在root用户(管理员用户)下执行的命令#打头的和前者相反,即r ...

  9. 前后端分离之 跨域和JWT

    书接上回:https://www.cnblogs.com/yangyuanhu/p/12081525.html 前后端分离案例 现在把自己当成是前端,要开发一个前后分离的简单页面,用于展示学生信息列表 ...

  10. [C/C++] 只允许程序运行一个实例

    原理是创建一个内核对象之后 如果再创建一个同名的对象 就会给代码中的GetLastError函数对应的变量修改为 ERROR_ALREADY_EXISTS (但是不影响"创建"对象 ...