-
[LeetCode] 1779. Find Nearest Point That Has the Same X or Y CoordinateLeetCode 2021. 10. 19. 11:39728x90
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/
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
Find Nearest Point That Has the Same X or Y Coordinate

주어진 x,y 좌표와 다른 좌표들이 있는 배열이 주어진다.
주어진 x,y 좌표와 x좌표가 같거나 y좌표가 같은 좌표들 중, 가장 가까운 거리를 출력하는 문제다.
풀이는
먼저 최소값 m을 생성한뒤,
배열에서 좌표를 하나씩 꺼내어
x좌표가 서로 같거나 y좌표가 서로 같은지 확인한 후, 최소값을 업데이트 시켜준다.
전체 풀이 코드
class Solution { public: int nearestValidPoint(int x, int y, vector<vector<int>>& points) { int midx = -1, m=INT_MAX; for (int i=0; i<points.size(); i++) { if ((points[i][0]==x || points[i][1]==y) && m>abs(points[i][0]-x) + abs(points[i][1]-y)) { midx = i; m = abs(points[i][0]-x) + abs(points[i][1]-y); } } return midx; } };'LeetCode' 카테고리의 다른 글
[LeetCode] 402. Remove K Digits (0) 2021.10.19 [LeetCode] 496. Next Greater Element I (0) 2021.10.19 [LeetCode] 1043. Partition Array for Maximum Sum (0) 2021.10.18 [LeetCode] 454. 4Sum II (0) 2021.10.18 [LeetCode] 18. 4Sum (0) 2021.10.18