LeetCode 5271. 访问所有点的最小时间 Minimum Time Visiting All Points
地址 https://leetcode-cn.com/problems/minimum-time-visiting-all-points/submissions/
题目描述
平面上有 n 个点,点的位置用整数坐标表示 points[i] = [xi, yi]。请你计算访问所有这些点需要的最小时间(以秒为单位)。
你可以按照下面的规则在平面上移动:
每一秒沿水平或者竖直方向移动一个单位长度,或者跨过对角线(可以看作在一秒内向水平和竖直方向各移动一个单位长度)。
必须按照数组中出现的顺序来访问这些点。
样例

示例1
输入:points = [[,],[,],[-,]]
输出:
解释:一条最佳的访问路径是: [,] -> [,] -> [,] -> [,] -> [,] -> [,] -> [,] -> [-,]
从 [,] 到 [,] 需要 秒
从 [,] 到 [-,] 需要 秒
一共需要 秒 示例2
输入:points = [[,],[-,]]
输出:
提示: points.length == n
<= n <=
points[i].length ==
- <= points[i][], points[i][] <=
算法1
若两点不在一条直线上 优先走斜线是最优的 同时减少横向和竖向的距离
走斜线意味着横向竖向同时增加相同单位
所以也意味着 走横向和竖向之差中 较小的值(走斜向)
然后再走横向和竖向之差中两个值得差(表示走竖向或者走横向)
C++ 代码
class Solution {
public:
int minTimeToVisitAllPoints(vector<vector<int>>& points) {
int sum = ;
for(int i = ; i < points.size()-;i++){
int xx = abs(points[i][] - points[i+][]);
int yy = abs(points[i][] - points[i+][]);
sum += min(xx,yy);
sum += max(xx,yy) - min(xx,yy);
}
return sum;
}
};

LeetCode 5271. 访问所有点的最小时间 Minimum Time Visiting All Points的更多相关文章
- leetcode.1266访问所有点的最小时间
平面上有 n 个点,点的位置用整数坐标表示 points[i] = [xi, yi].请你计算访问所有这些点需要的最小时间(以秒为单位). 你可以按照下面的规则在平面上移动: 每一秒沿水平或者竖直方向 ...
- leetcode 1266. Minimum Time Visiting All Points
On a plane there are n points with integer coordinates points[i] = [xi, yi]. Your task is to find th ...
- 【leetcode】1266. Minimum Time Visiting All Points
题目如下: On a plane there are n points with integer coordinates points[i] = [xi, yi]. Your task is to f ...
- hdu 5067 遍历指定点集最小时间
http://acm.hdu.edu.cn/showproblem.php?pid=5067 贴题解 由于Harry的dig machine是无限大的,而装载石头和卸载石头是不费时间的,所以问题可以转 ...
- java8 获取某天最大(23:59:59)和最小时间(00:00:00)
public class DateUtil { // 获得某天最大时间 2018-03-20 23:59:59 public static Date getEndOfDay(Date date) { ...
- LeetCode:访问所有节点的最短路径【847】
LeetCode:访问所有节点的最短路径[847] 题目描述 给出 graph 为有 N 个节点(编号为 0, 1, 2, ..., N-1)的无向连通图. graph.length = N,且只有节 ...
- [LeetCode] Next Closest Time 下一个最近时间点
Given a time represented in the format "HH:MM", form the next closest time by reusing the ...
- MS Chart-按照数据库的最大最小时间设置X轴label.
核心代码: Chart1.ChartAreas[0].AxisX.Interval = (Front_Max - Front_Min).Days / 2; Chart1.ChartAreas[0].A ...
- Python3基础 getatime getctime getmtime 文件的最近访问 + 属性修改 + 内容修改时间
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
随机推荐
- WPF之DataTemplateSelector的运用
本文主要记录WPF中DataTemplateSelector的运用,数据模板选择器主要运用在一些项容器中用于根据不同的数据类型选择不同的DataTemplate,以便展示不同的数据.在此以在listb ...
- How to: Apply Attributes to Entity Properties when Using Model First 如何:在ModelFirst时将属性应用于实体属性
In a Model First data model, object properties are declared in the designer-generated files, and you ...
- SSH框架之Spring第二篇
1.1 基于注解的IOC配置 既注解配置和xml配置要实现的功能都是一样的,都是要降低程序间的耦合.只是配置的形式不一样. 1.2 环境搭建 1.2.1 第一步:拷贝必备的jar包 需要多拷贝一个sp ...
- PHP命令执行漏洞初探
PHP命令执行漏洞初探 Mirror王宇阳 by PHP 命令执行 PHP提供如下函数用于执行外部应用程序:例如:system().shell_exec().exec().passthru() sys ...
- Android 弹出Dialog时隐藏状态栏和底部导航栏
上代码 dialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); di ...
- 坚果云+typora(个人十分喜欢的一个记笔记方式)
1.名称 坚果云 markdown--->typora 2.喜欢原因 2.1 坚果云 坚果云全平台覆盖,支持Windows.Mac.Linux.iOS(iPad及iPhone).Android. ...
- sqlldr bat遇到的问题
在编写sqlldr相关的bat脚本时,遇到执行bat后一直循环执行的问题,网上也有遇到相同问题的朋友: 链接:https://zhidao.baidu.com/question/17039912443 ...
- MAC下安装pomelo
配置:OS X 10.9.4 + Xcode 6.0 摘要:本文目标为成功运行pomelo的HelloWorld程序. 壹.| 安装必要项 一.安装Xcode及相关工具 1.安装Xcode. ...
- 网页前端之CSS学习记录总结篇
标签: 块级标签block:div, p, h1-h6, ul, ol,li, dl, dt, dd,table,tr等,独占一行,可以设置宽高,默认是父标签的100%:行内标签inline:a,sp ...
- 集合系列 List(四):LinkedList
LinkedList 是链表的经典实现,其底层采用链表节点的方式实现. public class LinkedList<E> extends AbstractSequentialList& ...