SQL/HackerRank

[HackerRank/SQL] Weather Observation Station 18

류진주 2021. 10. 30. 19:11

https://www.hackerrank.com/challenges/weather-observation-station-18/problem

 

Weather Observation Station 18 | HackerRank

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

www.hackerrank.com

Consider P1(a,b) and P2(c,d) to be two points on a 2D plane.

  •  happens to equal the minimum value in Northern Latitude (LAT_N in STATION).
  •  happens to equal the minimum value in Western Longitude (LONG_W in STATION).
  •  happens to equal the maximum value in Northern Latitude (LAT_N in STATION).
  •  happens to equal the maximum value in Western Longitude (LONG_W in STATION).

Query the Manhattan Distance between points  P1 and  P2 and round it to a scale of  decimal places.

Input Format

The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.


[풀이]

Manhattan distance는 |x1-x2| + |y1-y2| 의 공식으로 계산할 수 있다.

a는 MIN(LAT_N)b는 MIN(LONG_W)c는 MAX(LAT_N)d는 MAX(LONG_W) 이다.

Manhattan distance를 구하려면 |a-c| + |b-d| 인데 큰 값에서 작은 값을 빼주면 절댓값을 붙이지 않아도 양수이므로, 

c-a + d-b로 계산할 것이다.

구한 Manhattan distance를 소수점 5번째 자리에서 반올림할 것이므로 ROUND()를 이용하여 반올림해준다.

 

[코드]

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