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 ...
随机推荐
- 高内存 高CPU 劣质网络下的测试
内存 先把系统的虚拟内存去掉 (右键我的电脑属性里有的.选择那个无分页文件 虚拟内存在任务管理器就不显示了), 然后机子本身内存不高,开几个网页就满了 CPU cpu可以用鲁大师测试cpu ...
- navicat导入.csv表格
我本地的navicat不知道啥情况,导入不了表格,然后把表格转为.csv的,然后导入就好了 1.表格另存为.csv格式的 2.打开Navicat,选择要导入的表,然后右键->导入向导,选择.cs ...
- Java读取、写入、处理Excel文件中的数据(转载)
原文链接 在日常工作中,我们常常会进行文件读写操作,除去我们最常用的纯文本文件读写,更多时候我们需要对Excel中的数据进行读取操作,本文将介绍Excel读写的常用方法,希望对大家学习Java读写Ex ...
- go基础_数组
数组有2种赋值方式 一种明确指定长度,另一种从赋值数目指定长度 package main import "fmt" func main() { //数组赋值方式1,指定长度 arr ...
- 极客从CPU选择开始-CPU详解
先来看看CPU天梯图(来自(快科技CPU性能天梯图)[https://www.mydrivers.com/zhuanti/tianti/cpu/index.html]) Intel VS AMD (P ...
- 「JSOI2015」圈地
「JSOI2015」圈地 传送门 显然是最小割. 首先对于所有房子,权值 \(> 0\) 的连边 \(s \to i\) ,权值 \(< 0\) 的连边 \(i \to t\) ,然后对于 ...
- js加密(六)QB.com
1. url: https://notice.qb.com/detail?noticeId=256 2. target: 3. 分析: 3.1 打开网址,刷新页面,看看都发送了哪些请求. 看到了发送了 ...
- C# 重载与重写
重载(overload) 指调用同一方法名,但各方法中参数的数据类型.个数或顺序不同. public static int Add(int x,int y) { return x + y; } pub ...
- 喵星之旅-狂奔的兔子-myeclipse搭建ssm
. 可以使用试用期限内的myeclipse,也可以找到有授权的机器进行操作.搭建好的项目框架可以直接移植到免费软件eclipse使用.或者直接购买myeclipse授权. 一.创建一个java web ...
- Leader:这样的 Bug 你也写的出来???
Hello~各位读者新年好!不知道大家春节假期是否已延长,小黑哥刚接到通知,假期延长到 2 月 2 号,另外回去之后需要在家办公,自行隔离两周.还没试过在家办公,小黑哥就怕到时候生物钟还没调整过来,一 ...