leetcode菜鸡斗智斗勇系列(7)--- 用最小的时间访问所有的节点
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)--- 用最小的时间访问所有的节点的更多相关文章
- leetcode菜鸡斗智斗勇系列(4)--- 单一数字的乘积和总合的减法
1.原题: https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/ Given an i ...
- leetcode菜鸡斗智斗勇系列(3)--- Jewels and Stones珠宝和钻石
1.原题: https://leetcode.com/problems/jewels-and-stones/ You're given strings J representing the types ...
- leetcode菜鸡斗智斗勇系列(2)--- 把一个ipv4地址转换成一串数字
1.原题: https://leetcode.com/problems/defanging-an-ip-address/ 这道题本身很简单, Given a valid (IPv4) IP addre ...
- leetcode菜鸡斗智斗勇系列(1)---把一个链表中的二进制数字转换为一个整型数(int)
Convert Binary Number in a Linked List to Integer这道题在leetcode上面算作是“easy”,然而小生我还是不会做,于是根据大佬的回答来整理一下思路 ...
- leetcode菜鸡斗智斗勇系列(10)--- Decrypt String from Alphabet to Integer Mapping
1.原题: https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/submissions/ Giv ...
- 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 ...
- 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 ...
- leetcode菜鸡斗智斗勇系列(6)--- 检查一个string里面有几个对称的字段
1.原题: https://leetcode.com/problems/split-a-string-in-balanced-strings/ Split a String in Balanced S ...
- leetcode菜鸡斗智斗勇系列(5)--- 寻找拥有偶数数位的数字
1.原题: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/ Given an array nums of ...
随机推荐
- vs查看动态库依赖
dumpbin是VS自带的工具,进入Visual Studio 的命令提示 查看程序或动态库所依赖的动态库 dumpbin /dependents abc.exe 查看动态库的输出函数 dumpbi ...
- java获取配置文件中变量值
在resources 目录下新建config.properties文件 #文件保存路径 filePath=E:\\images\\file 工具类 public class ConfigUtil { ...
- 快递查询API
https://market.aliyun.com/products/56928004/cmapi014394.html#sku=yuncode839400000
- EVE-NG镜像模板资源占用统计
转:http://www.emulatedlab.com/forum.php?mod=viewthread&tid=432&extra=page%3D1 EVE Image fold ...
- linux日常运维工作
Linux的使用环境也日趋成熟,各种开源产品络绎不绝,大有百花齐放的盛景,那么当Linux落地企业,回归工作时,我们还要面对这Linux运维方面的诸多问题,今天我们特意组织一场有关Linux 在企业运 ...
- RocketMq-粪发涂墙1.0
角色 说明 Producer 生产者,用于将消息发送到RocketMQ,生产者本身既可以是生成消息,也可以对外提供接口,由外部来调用接口,再由生产者将受到的消息发送给MQ. Consumer 消费者, ...
- opencv python:图像梯度
一阶导数与Soble算子 二阶导数与拉普拉斯算子 图像边缘: Soble算子: 二阶导数: 拉普拉斯算子: import cv2 as cv import numpy as np # 图像梯度(由x, ...
- 吴裕雄 PYTHON 人工智能——基于MASK_RCNN目标检测(5)
import os import sys import numpy as np import tensorflow as tf import matplotlib import matplotlib. ...
- 浅谈hover用法
在前端页面制作中,我们时常要用到移动显示.隐藏的动态效果,我们一般采用js来实现此效果.不过在大部分情况下,我们也可以使用hover来实现此动态效果. 在此,我谈一谈我对hover的用法,请看以下代码 ...
- dp(武功秘籍)
众所周知,太吾绘卷是非常爱(niu)你(bi)的国产武侠游戏,里面有一个继承系统,当你死后可以在你的子孙中挑选一个继承人,用他的人物继续进行游戏.当你挑选继承人的时候一定会挑选能力最强,天赋最高的那一 ...