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 ...
随机推荐
- C# 委托汇总
委托汇总以及遗留问题: using System; using System.Collections.Generic; using System.Linq; using System.Text; us ...
- SQLyog连接MySQL8.0报2058错误的解决方案
引言 用SQLyog连接MySQL8.0(社区版:mysql-installer-community-8.0.15.0.msi),出现错误2058(Plugin caching_sha2_passwo ...
- digitalworld.local:Torment Vulnhub Walkthrough
主机层面扫描: ╰─ nmap -p1-65535 -sV -A 10.10.202.135Starting Nmap 7.70 ( https://nmap.org ) at 2019-08-09 ...
- 2、nio的例子实践
下面的例子,说明了,nio中的三大核心类的基本使用.buffer,channel,selector package com.shengsiyuan.nio; import org.junit.Test ...
- Abp zero 登录 添加腾讯云验证码
腾讯云验证码是为网页.App.小程序开发者提供的安全验证服务,基于腾讯多年的大数据积累和人工智能决策引擎,构建智能分级验证模型,最大程度保护业务安全的同时,提供更精细化的用户体验. 腾讯云--> ...
- Docker系列03-容器Docker镜像的使用
Docker镜像的使用前两个章节,介绍了容器的相关基础知识,这章我们介绍镜像的简单使用,镜像hub里面有来自于全世界贡献的各种镜像,包括一些入门和学习练手的镜像,今天我们使用的正式其中一个用于学习练习 ...
- Java_枚举Enum基本使用
特性 在某些情况下,一个类的对象时有限且固定的,如季节类,它只有春夏秋冬4个对象这种实例有限且固定的类,在 Java 中被称为枚举类: 在 Java 中使用 enum 关键字来定义枚举类,其地位与 c ...
- facl 用户以及Linux 终端
FACL : Filesystem Access Control List 利用文件扩展保存额外的访问控制权限setfacl -b:Remove all -m:设定 u:UID:perm g:GID: ...
- Paint.NET软件分享
date: 2019-07-26 下载链接 官网链接 这是一款类Photoshop的轻量级图片编辑软件,仅有8.7MB.不多说话,直接上链接. 百度网盘链接 提取码:v4b2 软件简介 (百度百科警告 ...
- "echo 0 /proc/sys/kernel/hung_task_timeout_secs" disable this message
问题现象: 问题原因: 默认情况下, Linux会最多使用40%的可用内存作为文件系统缓存.当超过这个阈值后,文件系统会把将缓存中的内存全部写入磁盘, 导致后续的IO请求都是同步的. 将缓存写入磁盘时 ...