Class Solution
java.lang.Object
g1701_1800.s1779_find_nearest_point_that_has_the_same_x_or_y_coordinate.Solution
1779 - Find Nearest Point That Has the Same X or Y Coordinate.<p>Easy</p>
<p>You are given two integers, <code>x</code> and <code>y</code>, which represent your current location on a Cartesian grid: <code>(x, y)</code>. You are also given an array <code>points</code> where each <code>points[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents that a point exists at <code>(a<sub>i</sub>, b<sub>i</sub>)</code>. A point is <strong>valid</strong> if it shares the same x-coordinate or the same y-coordinate as your location.</p>
<p>Return <em>the index <strong>(0-indexed)</strong> of the <strong>valid</strong> point with the smallest <strong>Manhattan distance</strong> from your current location</em>. If there are multiple, return <em>the valid point with the <strong>smallest</strong> index</em>. If there are no valid points, return <code>-1</code>.</p>
<p>The <strong>Manhattan distance</strong> between two points <code>(x<sub>1</sub>, y<sub>1</sub>)</code> and <code>(x<sub>2</sub>, y<sub>2</sub>)</code> is <code>abs(x<sub>1</sub> - x<sub>2</sub>) + abs(y<sub>1</sub> - y<sub>2</sub>)</code>.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]]</p>
<p><strong>Output:</strong> 2</p>
<p><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.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> x = 3, y = 4, points = [[3,4]]</p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong> The answer is allowed to be on the same location as your current location.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> x = 3, y = 4, points = [[2,3]]</p>
<p><strong>Output:</strong> -1</p>
<p><strong>Explanation:</strong> There are no valid points.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>4</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>1 <= x, y, a<sub>i</sub>, b<sub>i</sub> <= 10<sup>4</sup></code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
nearestValidPoint
public int nearestValidPoint(int x, int y, int[][] points)
-