@@ -0,0 +1,2502 @@
+//  Copyright 2021 The KeepTry Open Source Project%0A//%0A// Licensed under the Apache License, Version 2.0 (the %22License%22);%0A// you may not use this file except in compliance with the License.%0A// You may obtain a copy of the License at%0A//%0A//     http://www.apache.org/licenses/LICENSE-2.0%0A//%0A// Unless required by applicable law or agreed to in writing, software%0A// distributed under the License is distributed on an %22AS IS%22 BASIS,%0A// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A// See the License for the specific language governing permissions and%0A// limitations under the License.%0A//%0A%0Apackage array;%0A%0Apublic class Leetcode1779FindNearestPointThatHastheSameXorYCoordinate %7B%0A  /*%0A     1779. Find Nearest Point That Has the Same X or Y Coordinate%0A%0A%0A    You are given two integers, x and y, which represent your current location on a%0A    Cartesian grid: (x, y). You are also given an array points where each points%5Bi%5D = %5Bai, bi%5D%0A    represents that a point exists at (ai, bi). A point is valid if it shares the%0A    same x-coordinate or the same y-coordinate as your location.%0A%0A    Return the index (0-indexed) of the valid point with the smallest%0A    Manhattan distance from your current location.%0A    If there are multiple, return the valid point with the smallest index.%0A    If there are no valid points, return -1.%0A%0A    The Manhattan distance between two points%0A    (x1, y1) and (x2, y2) is abs(x1 - x2) + abs(y1 - y2).%0A%0A%0A    Input: x = 3, y = 4, points = %5B%5B1,2%5D,%5B3,1%5D,%5B2,4%5D,%5B2,3%5D,%5B4,4%5D%5D%0A    Output: 2%0A    Explanation: Of all the points, only %5B3,1%5D, %5B2,4%5D and %5B4,4%5D%0A    are valid. Of the valid points, %5B2,4%5D and %5B4,4%5D have the smallest%0A    Manhattan distance from your current location, with a distance of 1.%0A    %5B2,4%5D has the smallest index, so return 2.%0A%0A%0A    Input: x = 3, y = 4, points = %5B%5B3,4%5D%5D%0A    Output: 0%0A    Explanation: The answer is allowed to be on the same location as%0A    your current location.%0A%0A%0A    Input: x = 3, y = 4, points = %5B%5B2,3%5D%5D%0A    Output: -1%0A    Explanation: There are no valid points.%0A%0A    Constraints:%0A%0A        1 %3C= points.length %3C= 104%0A        points%5Bi%5D.length == 2%0A        1 %3C= x, y, ai, bi %3C= 104%0A  */%0A  public int nearestValidPoint(int x, int y, int%5B%5D%5B%5D points) %7B%0A    int a = -1, min = Integer.MAX_VALUE;%0A    for (int i = 0; i %3C points.length; ++i) %7B%0A      int dx = x - points%5Bi%5D%5B0%5D, dy = y - points%5Bi%5D%5B1%5D;%0A      if (dx * dy == 0 && Math.abs(dy + dx) %3C min) %7B%0A        min = Math.abs(dx + dy);%0A        a = i;%0A      %7D%0A    %7D%0A    return a;%0A  %7D%0A%7D%0A

Leetcode 1779. Find Nearest Point That Has the Same X or Y Coordinate