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

边栏推荐
- QML reported an error expected token ";", expected a qualified name ID
- Granularity of blocking of concurrency control
- 微服务链路风险分析
- 每日刷题记录 (十四)
- Net small and medium-sized enterprise project development framework series (one)
- Huawei fast game failed to call the login interface, and returned error code -1
- Summary of El and JSTL precautions
- ICMP 介绍
- Leetcode simple question: check whether each row and column contain all integers
- EL与JSTL注意事项汇总
猜你喜欢

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

The simple problem of leetcode is to split a string into several groups of length K

Shell script, awk uses if, for process control

Huawei fast game failed to call the login interface, and returned error code -1

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

AD637使用筆記

Index optimization of performance tuning methodology

Sentinel production environment practice (I)

Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)

Business learning of mall commodity module
随机推荐
Oracle views the data size of a table
Did you brush the real title of the blue bridge cup over the years? Come here and teach you to counter attack!
Stored procedures and stored functions
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
Regular expressions and re Libraries
Installation of VMware Workstation
Oracle advanced query
POJ 3237 tree (tree chain splitting)
Drawing HSV color wheel with MATLAB
Cross end solutions to improve development efficiency
Leetcode simple question: check whether each row and column contain all integers
华为云ModelArts文本分类–外卖评论
Storage optimization of performance tuning methodology
How can Huawei online match improve the success rate of player matching
Database tuning solution
Reptile practice
Sentinel production environment practice (I)
华为游戏多媒体调用切换房间方法出现异常Internal system error. Reason:90000017
Interview questions for basic software testing
regular expression