LeetCode_Study_Plan/Programming Skills

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

개발하는루루 2022. 9. 19. 22:04

https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/?envType=study-plan&id=programming-skills-i 

 

Find Nearest Point That Has the Same X or Y Coordinate - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

class Solution {
    public int nearestValidPoint(int x, int y, int[][] points) {
        int index = -1;
        int distance = Integer.MAX_VALUE;
        
        for (int i = 0; i < points.length; i++) {
            if (x == points[i][0] || y == points[i][1]) {
                int current_dis = Math.abs(x - points[i][0]) + Math.abs(y - points[i][1]);
                if (current_dis < distance) {
                    distance = current_dis;
                    index = i;
                }
            }
        }
        
        return index;
    }
}