当前位置:网站首页>Leetcode simple question: find the nearest point with the same X or Y coordinate
Leetcode simple question: find the nearest point with the same X or Y coordinate
2022-07-05 22:03:00 【·Starry Sea】
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
source : Power button (LeetCode)
Their thinking
According to the meaning of the topic , Find the effective point first . The definition of effective point is points The element of has one or two with a given x and y It corresponds to equality , Then the current element is a valid point . Then calculate the distance between the given point and the effective point , Count the nearest valid point and return the subscript .
class Solution:
def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:
distance=float('inf')
index=-1
for i in range(len(points)):
if x==points[i][0] or y==points[i][1]:
if distance>abs(points[i][0]-x)+abs(points[i][1]-y):
distance=abs(points[i][0]-x)+abs(points[i][1]-y)
index=i
return index

边栏推荐
- 微服務鏈路風險分析
- Oracle triggers
- Comment développer un plug - in d'applet
- 【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
- U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程
- 装饰器学习01
- K210 learning notes (IV) k210 runs multiple models at the same time
- 如何组织一场实战攻防演练
- ICMP 介绍
- Did you brush the real title of the blue bridge cup over the years? Come here and teach you to counter attack!
猜你喜欢

AD637使用筆記
![[Yugong series] go teaching course in July 2022 004 go code Notes](/img/56/d596e7c7bec9abd888e8f18f9769f8.png)
[Yugong series] go teaching course in July 2022 004 go code Notes

matlab绘制hsv色轮图

装饰器学习01

PyGame practical project: write Snake games with 300 lines of code

K210 learning notes (IV) k210 runs multiple models at the same time

Create a virtual machine on VMware (system not installed)

Analysis and test of ModbusRTU communication protocol

Oracle triggers

华为联机对战如何提升玩家匹配成功几率
随机推荐
Reptile practice
Oracle triggers
Multiplexing of Oracle control files
Net small and medium-sized enterprise project development framework series (one)
Learning of mall permission module
Decorator learning 01
Huawei fast game failed to call the login interface, and returned error code -1
如何開發引入小程序插件
AD637 usage notes
华为游戏多媒体调用切换房间方法出现异常Internal system error. Reason:90000017
MMAP learning
Robot operation mechanism
854. String BFS with similarity K
华为快游戏调用登录接口失败,返回错误码 -1
如何组织一场实战攻防演练
PIP install beatifulsoup4 installation failed
Kingbasees v8r3 cluster maintenance case -- online addition of standby database management node
让开发效率提升的跨端方案
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
Yolov5 training custom data set (pycharm ultra detailed version)