package leetcode;

/**
 * https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/
 */
public class Problem1779 {
    public int nearestValidPoint(int x, int y, int[][] points) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1779 prob = new Problem1779();
        System.out.println(prob.nearestValidPoint(3, 4, new int[][]{{1,2},{3,1},{2,4},{2,3},{4,4}})); // 2
        System.out.println(prob.nearestValidPoint(3, 4, new int[][]{{3,4}})); // 0
        System.out.println(prob.nearestValidPoint(3, 4, new int[][]{{2,3}})); // -1
    }
}

@@ -213,405 +213,787 @@
     
-// TODO%0A        return 0;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1779 prob = new Problem1779();%0A        System.out.println(prob.nearestValidPoint(3, 4, new int%5B%5D%5B%5D%7B%7B1,2%7D,%7B3,1%7D,%7B2,4%7D,%7B2,3%7D,%7B4,4%7D%7D)); // 2%0A        System.out.println(prob.nearestValidPoint(3, 4, new int%5B%5D%5B%5D%7B%7B3,4%7D%7D)); // 0%0A        System.out.println(prob.nearestValidPoint(3, 4, new int%5B%5D%5B%5D%7B%7B2,3%7D%7D)); // -1
+int answer = -1;%0A        int min = Integer.MAX_VALUE;%0A        for (int i = 0; i %3C points.length; i++) %7B%0A            int%5B%5D point = points%5Bi%5D;%0A            if (point%5B0%5D == x) %7B%0A                int distance = manhattan(x, y, point%5B0%5D, point%5B1%5D);%0A                if (distance %3C min) %7B%0A                    min = distance;%0A                    answer = i;%0A                %7D%0A            %7D%0A            if (point%5B1%5D == y) %7B%0A                int distance = manhattan(x, y, point%5B0%5D, point%5B1%5D);%0A                if (distance %3C min) %7B%0A                    min = distance;%0A                    answer = i;%0A                %7D%0A            %7D%0A        %7D%0A        return answer;%0A    %7D%0A%0A    private static int manhattan(int x1, int y1, int x2, int y2) %7B%0A        return Math.abs(x1 - x2) + Math.abs(y1 - y2);
 %0A   

Solve problem 1779