{"id":8192,"date":"2021-03-06T09:02:48","date_gmt":"2021-03-06T17:02:48","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8192"},"modified":"2021-03-06T09:04:49","modified_gmt":"2021-03-06T17:04:49","slug":"leetcode-1779-find-nearest-point-that-has-the-same-x-or-y-coordinate","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-1779-find-nearest-point-that-has-the-same-x-or-y-coordinate\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1779. Find Nearest Point That Has the Same X or Y Coordinate"},"content":{"rendered":"\n<p>You are given two integers,&nbsp;<code>x<\/code>&nbsp;and&nbsp;<code>y<\/code>, which represent your current location on a Cartesian grid:&nbsp;<code>(x, y)<\/code>. You are also given an array&nbsp;<code>points<\/code>&nbsp;where each&nbsp;<code>points[i] = [a<sub>i<\/sub>, b<sub>i<\/sub>]<\/code>&nbsp;represents that a point exists at&nbsp;<code>(a<sub>i<\/sub>, b<sub>i<\/sub>)<\/code>. A point is&nbsp;<strong>valid<\/strong>&nbsp;if it shares the same x-coordinate or the same y-coordinate as your location.<\/p>\n\n\n\n<p>Return&nbsp;<em>the index&nbsp;<strong>(0-indexed)<\/strong>&nbsp;of the&nbsp;<strong>valid<\/strong>&nbsp;point with the smallest&nbsp;<strong>Manhattan distance<\/strong>&nbsp;from your current location<\/em>. If there are multiple, return&nbsp;<em>the valid point with the&nbsp;<strong>smallest<\/strong>&nbsp;index<\/em>. If there are no valid points, return&nbsp;<code>-1<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<strong>Manhattan distance<\/strong>&nbsp;between two points&nbsp;<code>(x<sub>1<\/sub>, y<sub>1<\/sub>)<\/code>&nbsp;and&nbsp;<code>(x<sub>2<\/sub>, y<sub>2<\/sub>)<\/code>&nbsp;is&nbsp;<code>abs(x<sub>1<\/sub>&nbsp;- x<sub>2<\/sub>) + abs(y<sub>1<\/sub>&nbsp;- y<sub>2<\/sub>)<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> Of all the points, only [3,1], [2,4] and [4,4] are valid. Of the valid points, [2,4] and [4,4] have the smallest Manhattan distance from your current location, with a distance of 1. [2,4] has the smallest index, so return 2.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> x = 3, y = 4, points = [[3,4]]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> The answer is allowed to be on the same location as your current location.<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> x = 3, y = 4, points = [[2,3]]\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> There are no valid points.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= points.length &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>points[i].length == 2<\/code><\/li><li><code>1 &lt;= x, y, a<sub>i<\/sub>, b<sub>i<\/sub>&nbsp;&lt;= 10<sup>4<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n    int min_dist = INT_MAX;\n    int ans = -1;\n    for (int i = 0; i < points.size(); ++i) {\n      const int dx = abs(points[i][0] - x);\n      const int dy = abs(points[i][1] - y);      \n      if (dx * dy == 0 &#038;&#038; dx + dy < min_dist) {\n        min_dist = dx + dy;\n        ans = i;\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two integers,&nbsp;x&nbsp;and&nbsp;y, which represent your current location on a Cartesian grid:&nbsp;(x, y). You are also given an array&nbsp;points&nbsp;where each&nbsp;points[i] = [ai, bi]&nbsp;represents&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[127],"tags":[20,222,284],"class_list":["post-8192","post","type-post","status-publish","format-standard","hentry","category-geometry","tag-array","tag-easy","tag-geometry","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8192"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8192\/revisions"}],"predecessor-version":[{"id":8194,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8192\/revisions\/8194"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}