当前位置:网站首页>Li Kou daily question - day 31 -1779 Find the nearest point with the same X or Y coordinate
Li Kou daily question - day 31 -1779 Find the nearest point with the same X or Y coordinate
2022-06-29 12:10:00 【Chongyou research Sen】
2022.6.29 Did you brush the questions today ?
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) .
analysis :
To give you one (x,y) And a two-dimensional array , You need to make sure that points and arrays are on a straight line , If meet , Find the smallest Manhattan distance , Then return to the first such minimum value in the array .
Ideas : Traversing a two-dimensional array , Find the minimum , Update axis . If there are two equally small numbers , Only the first one is recorded
analysis :
1. Violent solution
class Solution {
public:
int nearestValidPoint(int x, int y, vector<vector<int>>& points) {
int res = 0;
int ans = -1;
int max = 1000000;
for (auto i = 0; i < points.size(); i++)
{
if (points[i][0] == x || points[i][1] == y)
{
res = abs(points[i][0] - x) + abs(points[i][1]-y);
if (res < max)
{
max = res;
ans = i;
}
}
}
return ans;
}
};边栏推荐
- 深入理解 volatile 关键字
- MMdet的Resnet卷积替换成Ghost卷积组所出现的问题
- What are outer chain and inner chain?
- Ensure drinking water safety! Panyu Shawan waterway water quality online monitoring system passed the acceptance
- leetcode刷题:字符串07(重复的子字符串)
- 联想领像 lenovoimage 部分打印机 驱动 PPD 文件
- 初次使用 eolink 感受
- [pbootcms模板]作文网站/文档下载网站源码
- Helping the ultimate experience, best practice of volcano engine edge computing
- 【毕业季】总结过去,展望未来
猜你喜欢

Rebuild confidence in China's scientific research - the latest nature index 2022 released that China's research output increased the most

年轻就要醒着拼,年轻就要勇于尝试

每周推荐短视频:爱因斯坦是怎样思考问题的?
联想领像 lenovoimage 部分打印机 驱动 PPD 文件

Pytorch - 分布式通信原语(附源码)

黑化的蜜雪冰城,凭营销就想抓牢消费者的心?

信息技术应用创新专业人员(数据库)中级培训火热招生中(7月6-10日)

iMile 利用 Zadig 多云环境周部署千次,跨云跨地域持续交付全球业务

跟着官方学电机,BLDC两种控制策略,学到即赚到

Initial use of eolink
随机推荐
Helping the ultimate experience, best practice of volcano engine edge computing
Nature | biosynthetic potential of global marine microbiome
mysql截取字符串去重,mysql 截取字符串 去重 拼接
2022 Guangxi provincial safety officer C certificate examination questions and mock examination
Sofaregistry source code | data synchronization module analysis
又一所“省会大学”,来了!
Numpy's ndarray array Foundation
Numpy's ndarray array Foundation
Zhengda futures liu4 data integration
MariaDB的安装与配置
跟着官方学电机,BLDC两种控制策略,学到即赚到
杰理之发起对耳配对、回连、开启可发现、可连接的轮循函数【篇】
Jerry's about TWS pairing mode configuration [chapter]
How to obtain method parameter values through WinDbg
杰理之关于 TWS 配对方式配置【篇】
Installation and configuration of MariaDB
AOSP ~ 初始化语言
毕业5年,我问遍了身边的大佬,总结了他们的学习方法
杰理之关于 TWS 配对方式配置【篇】
ES6 数组方法