当前位置:网站首页>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
边栏推荐
- Installation of VMware Workstation
- Recovery technology with checkpoints
- How to organize an actual attack and defense drill
- Oracle checkpoint queue - Analysis of the principle of instance crash recovery
- Deeply convinced plan X - network protocol basic DNS
- 笔记本电脑蓝牙怎么用来连接耳机
- Evolution of large website architecture and knowledge system
- A trip to Suzhou during the Dragon Boat Festival holiday
- AD637 usage notes
- NET中小型企业项目开发框架系列(一个)
猜你喜欢
华为游戏多媒体调用切换房间方法出现异常Internal system error. Reason:90000017
Sentinel production environment practice (I)
PyGame practical project: write Snake games with 300 lines of code
CA certificate trampled pit
Decorator learning 01
Kingbasees v8r3 cluster maintenance case -- online addition of standby database management node
Serializability of concurrent scheduling
Win11运行cmd提示“请求的操作需要提升”的解决方法
MMAP
Learning of mall permission module
随机推荐
数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
Win11运行cmd提示“请求的操作需要提升”的解决方法
How to organize an actual attack and defense drill
Deeply convinced plan X - network protocol basic DNS
资深电感厂家告诉你电感什么情况会有噪音电感噪音是比较常见的一种电感故障情况,如果使用的电感出现了噪音大家也不用着急,只需要准确查找分析出什么何原因,其实还是有具体的方法来解决的。作为一家拥有18年品牌
poj 3237 Tree(樹鏈拆分)
A long's perception
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
K210 learning notes (IV) k210 runs multiple models at the same time
ICMP introduction
Serializability of concurrent scheduling
Form artifact
Livelocks and deadlocks of concurrency control
Recovery technology with checkpoints
Oracle triggers
微服务链路风险分析
Oracle checkpoint queue - Analysis of the principle of instance crash recovery
Detailed explanation of memset() function usage
Summary of El and JSTL precautions
Win11缺少dll文件怎么办?Win11系统找不到dll文件修复方法