코딩 테스트/MySQL

[HackerRank] Weather Observation Station 18

알밤바 2025. 8. 14. 11:43
728x90
반응형
 

Weather Observation Station 18 | HackerRank

Query the Manhattan Distance between two points, round or truncate to 4 decimal digits.

www.hackerrank.com


문제

 

문제 접근 방식

이번 문제는 아주 간단한 문제이다. SQL 함수만 알면 됨!

맨해튼 거리 (Manhattan Distance) 공식도 알고 있어야 하는데, 문제에서 맨해튼 거리에 대한 설명이 있는 링크가 걸려있어 참고하면 된다.

 

맨해튼 거리

축을 따라 직각으로 측정된 두 점 사이의 거리이다.

(a, b)에서 p1을, (c, d)에서 p2를 갖는 평면에서는 |a - c| + |b - d|로 맨해튼 거리를 구할 수 있다.

 

Manhattan distance

Definition: The distance between two points measured along axes at right angles. In a plane with p1 at (x1, y1) and p2 at (x2, y2), it is |x1 - x2| + |y1 - y2|. Note: This is easily generalized to higher dimensions. Manhattan distance is often used in inte

xlinux.nist.gov

 

문제를 보면, p1(a, b) p2(c, d)이고 각각 a, b, c, d는 아래의 값을 의미한다.
a : 북위 (lat_n)의 최솟값
b : 서경 (long_w)의 최솟값
c : 북위 (lat_n)의 최댓값
d : 서경 (long_w)의 최댓값

 

1) 맨해튼 거리를 구하는 공식은 |a - c| + |b - d|으로 절대값을 구해야 하기에 절대값을 구하는 함수 abs()를 활용하면 된다.

2) 소숫점 4자리로 반올림 해야 하므로 round() 함수를 활용하면 된다.

SELECT ROUND(ABS(MIN(LAT_N) - MAX(LAT_N)) + ABS(MIN(LONG_W) - MAX(LONG_W)), 4)
FROM STATION
;

 

 

728x90
반응형

'코딩 테스트 > MySQL' 카테고리의 다른 글

[HackerRank] Weather Observation Station 19  (0) 2025.08.20
[HackerRank] Interviews  (2) 2025.08.18
[HackerRank] Occupations  (3) 2025.08.12
[HackerRank] Symmetric Pairs  (2) 2025.08.11
[HackerRank] Placements  (2) 2025.08.08