当前位置:网站首页>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;
}
}
边栏推荐
- Record number of visits yesterday
- 线程同步之信号量
- Good luck today
- How to split microservices
- Learning records of numpy Library
- Pre training weekly issue 51: reconstruction pre training, zero sample automatic fine tuning, one click call opt
- 爱可可AI前沿推介(6.27)
- Pytorch learning 3 (test training model)
- 【业务安全-04】万能用户名及万能密码实验
- Does Xinhua San still have to rely on ICT to realize its 100 billion enterprise dream?
猜你喜欢

Cesium realizes satellite orbit detour

Quick news: Huawei launched the Hongmeng developer competition; Tencent conference released the "Wanshi Ruyi" plan

每日刷题记录 (六)

What kind of air conditioner is this?

深入理解位运算

Kyndryl与Oracle和Veritas达成合作

AXI總線

Infiltration learning diary day20

Axi bus

美国芯片再遭重击,继Intel后又一家芯片企业将被中国芯片超越
随机推荐
[a complete human-computer interface program framework]
快速搭建一个自己的访问国外网站,搭建ss并开启bbr快速上网
After the deployment is created, the pod problem handling cannot be created
每日3题(2):检查二进制字符串字段
Read a poem
[weekly replay] the 81st biweekly match of leetcode
【问题解决】Tensorflow中run究竟运行了哪些节点?
How to solve the problem of missing language bar in win10 system
Yuweng information, a well-known information security manufacturer, joined the dragon lizard community to build an open source ecosystem
创建Deployment后,无法创建Pod问题处理
高德地图IP定位2.0备份
Summary and Thinking on interface test automation
[安洵杯 2019]Attack
Rereading the classic: the craft of research (1)
Dynamic Networks and Conditional Computation论文简读和代码合集
AXI总线
新华三的千亿企业梦,还得靠吃ICT老本来实现?
Axi bus
【业务安全-02】业务数据安全测试及商品订购数量篡改实例
国产数据库乱象