当前位置:网站首页>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

边栏推荐
- The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
- Did you brush the real title of the blue bridge cup over the years? Come here and teach you to counter attack!
- 多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
- MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server
- Stored procedures and stored functions
- A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
- Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
- 笔记本电脑蓝牙怎么用来连接耳机
- Web3为互联网带来了哪些改变?
- Pl/sql basic case
猜你喜欢

Oracle triggers

Analysis and test of ModbusRTU communication protocol

华为联机对战如何提升玩家匹配成功几率

Win11运行cmd提示“请求的操作需要提升”的解决方法

AD637 usage notes

U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程

Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer

MATLAB | App Designer·我用MATLAB制作了一款LATEX公式实时编辑器

装饰器学习01

Oracle checkpoint queue - Analysis of the principle of instance crash recovery
随机推荐
从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
Livelocks and deadlocks of concurrency control
微服務鏈路風險分析
Evolution of large website architecture and knowledge system
Analyse des risques liés aux liaisons de microservices
华为游戏多媒体服务调用屏蔽指定玩家语音方法,返回错误码3010
Bitbucket installation configuration
MMAP
The simple problem of leetcode is to split a string into several groups of length K
How to view Apache log4j 2 remote code execution vulnerability?
MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server
Interprocess communication in the "Chris Richardson microservice series" microservice architecture
Poj 3237 Tree (Tree Chain Split)
Blocking of concurrency control
装饰器学习01
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
Microservice link risk analysis
Regular expressions and re Libraries
Ad637 notes d'utilisation
database mirroring