当前位置:网站首页>Daily 3 questions (1): find the nearest point with the same X or Y coordinate
Daily 3 questions (1): find the nearest point with the same X or Y coordinate
2022-06-27 13:47:00 【Programmed ape without hair loss 2】
subject :
Here are two integers x and y , It means that you are in a Cartesian coordinate system (x, y) It's about . meanwhile , Give you an array in the same coordinate system points , among points[i] = [ai, bi] It means that (ai, bi) There's a point in the middle . When a point has the same... As where you are x Coordinates or the same y Coordinates , We call this point Effective .
Please return to your current position Manhattan distance Current It works Subscript of point ( Subscript from 0 Start ). If there are multiple nearest valid points , Please return to subscript Minimum One of the . If it doesn't work , Please return -1 .
Two points (x1, y1) and (x2, y2) Between Manhattan distance by abs(x1 - x2) + abs(y1 - y2) .
Example 1:
Input :x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]]
Output :2
explain : Of all the points ,[3,1],[2,4] and [4,4] It's effective . In effect ,[2,4] and [4,4] Manhattan is the closest to your current location , All for 1 .[2,4] The lowest subscript of , So back 2 .
Example 2:
Input :x = 3, y = 4, points = [[3,4]]
Output :0
Tips : The answer can be the same as your current position .
Example 3:
Input :x = 3, y = 4, points = [[2,3]]
Output :-1
explain : No, Effective point .
Tips :
1 <= points.length <= 10^4
points[i].length == 2
1 <= x, y, ai, bi <= 10^4
Ideas :
Traverse , Record valid points , Retain the subscript of the minimum value
java Code :
class Solution {
public int nearestValidPoint(int x, int y, int[][] points) {
int m = points.length;
int minLength = Integer.MAX_VALUE;
int minPoint = -1;
for (int i = 0; i < m; i++) {
if (x == points[i][0] || y == points[i][1]) {
if (minLength > Math.abs(x - points[i][0]) + Math.abs(y - points[i][1])) {
minLength = Math.abs(x - points[i][0]) + Math.abs(y - points[i][1]);
minPoint = i;
}
}
}
return minPoint;
}
}
边栏推荐
- 打印输出数(递归方法解决)
- IJCAI 2022 | greatly improve the effect of zero sample learning method with one line of code. Nanjing Institute of Technology & Oxford proposed the plug and play classifier module
- 全球芯片市场或陷入停滞,中国芯片逆势扩张加速提升自给率
- A pang's operation record
- scrapy
- Learning records of numpy Library
- 【mysql进阶】MTS主从同步原理及实操指南(七)
- [WUSTCTF2020]girlfriend
- Good luck today
- 外部存储器
猜你喜欢

一次性彻底解决 Web 工程中文乱码问题

【PHP代码注入】PHP语言常见可注入函数以及PHP代码注入漏洞的利用实例

高效率取幂运算
![[WUSTCTF2020]girlfriend](/img/a8/33fe5feb7bcbb73ba26a94d226cc4d.png)
[WUSTCTF2020]girlfriend

每日刷題記錄 (六)

How to solve the problem of missing language bar in win10 system

Axi bus

Learning records of numpy Library

Crane: a new way of dealing with dictionary items and associated data

How to set postman to Chinese? (Chinese)
随机推荐
How to set the compatibility mode of 360 speed browser
Shell 简明教程
全球芯片市场或陷入停滞,中国芯片逆势扩张加速提升自给率
【周赛复盘】LeetCode第81场双周赛
美国芯片再遭重击,继Intel后又一家芯片企业将被中国芯片超越
Using FRP tool to realize intranet penetration
解析Activity启动-生命周期角度
打印输出数(递归方法解决)
【业务安全-01】业务安全概述及测试流程
How to choose LAN instant messaging software
Can flush open an account for stock trading? Is it safe?
Prometheus 2.26.0 新特性
每日3题(2):检查二进制字符串字段
Does Xinhua San still have to rely on ICT to realize its 100 billion enterprise dream?
Daily question brushing record (6)
微服务如何拆分
Prometheus 2.26.0 new features
招标公告:暨南大学附属第一医院Oracle数据库维保服务采购
With the advent of the era of Internet of everything, Ruijie released a scenario based wireless zero roaming scheme
创建Deployment后,无法创建Pod问题处理