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 ...
随机推荐
- SpringBoot+idea搭建微服务简化流程
作者:个人微信公众号:程序猿的月光宝盒 1.新建普通maven工程 2.在父级pom中按需修改 3.删除父级src目录 4.创建公共模块common,里面只有service接口和实体类 5.构建微服务 ...
- MySql报错(持续更新)
目录 MySql报错 1. 重复键报错1062- duplicate entry '0' for key 'xxx' 1.1 报错场景 1.2 报错原因 1.3 解决方法 1.4 具体举例 2. VS ...
- VUE程序调试的方法
目录 VUE程序调试的方法 1.写本文的背景 2.调试与测试 3.Console调试法 3.1 添加console.log指令 3.2 调出温度界面如下 3.3 Google浏览器的Console窗口 ...
- 设计模式之单例模式(Java)
单例模式 问题 多线程操作同一对象保证对象的一致性 解决思路 只有一次实例化过程,产生一个实例化对象,并提供返回该对象的方法. 单例模式的分类 1 饿汉式 在加载类的时候就产生实例对象 public ...
- 分布式缓存 Redis 集群搭建
Redis 集群简介 Redis Cluster 即 Redis 集群,是 Redis 官方在 3.0 版本推出的一套分布式存储方案.完全去中心化,由多个节点组成,所有节点彼此互联.Redis 客户端 ...
- 2019 DevOps 技术指南
原文链接:https://hackernoon.com/the-2018-devops-roadmap-31588d8670cb 原文作者:javinpaul 翻译君:CODING 戴维奥普斯 写在前 ...
- TP事物的写法
Db::startTrans(); try{ Db::commit(); } catch (\Exception $e) { Db::rollback(); } use think\Db; publi ...
- 使用vsphere client 克隆虚拟机的具体操作步骤
采用 VMWare ESXi6.0,使用ESXi经常会遇到这样的问题,需要建立多个虚拟机,都是一样的操作系统,但是如果一台台装效率太低. VMware ESXi.VMware vCenter Serv ...
- Linux下安装Redis以及遇到的问题
参考链接:https://www.cnblogs.com/zdd-java/p/10288734.html https://www.cnblogs.com/uncleyong/p/9882843.ht ...
- 集合系列 Set(六):HashSet
HashSet 是 Set 集合的哈希实现,其继承了 AbstractSet 抽象类,并实现了 Set 接口. public class HashSet<E> extends Abstra ...