💻 문제 주소 : Weather Observation Station 18 | HackerRank
The PADS | HackerRank
Query the name and abbreviated occupation for each person in OCCUPATIONS.
www.hackerrank.com
💻 문제
Consider P1(a, b) and P2(c, d) to be two points on a 2D plane.
-. a happens to equal the minimum value in Northern Latitude (LAT_N in STATION).
-. b happens to equal the minimum value in Western Longitude (LONG_W in STATION).
-. c happens to equal the mzximum value in Northern Latitude (LAT_N in STATION).
-. d 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 4 decimal places.
Column name | Type |
ID | NUMBER |
CITY | VARCHAR2(21) |
STATE | VARCHAR2(2) |
LAT_N | NUMBER |
LONG_W | NUMBER |
💡 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|.
💻 코드 미리보기
* 본 문제는 MYSQL로 작성했습니다.
select round(abs(min(lat_n)-max(lat_n))+abs(min(long_w)-max(long_w)), 4)
from STATION
💻 문제 풀이
문제에서 요구하는 조건은 아래와 같다.
1️⃣ Query the Manhattan Distance between P1 and P2
2️⃣ Round it to a scale of 4 decimal places
1️⃣ Query the Manhattan Distance between P1 and P2
본 문제에서 Manhattan Distance는 |x1 - x2| + |y1 - y2| 라고 정의한다. 우리는 문제에서 P1과 P2에 대한 (x1, x2), (y1, y2) 값을 각각 LAT_N과 LONG_W에서 구하라고 했으니 이를 그대로 대입하면 된다.
이때 정의에 의하면 값은 각 x, y 연산은 절대값으로 나타내야 하기 때문에 절대값을 구하는 함수인 abs()를 써준다.
-. abs(Num) -- Num 자리에 수식을 써도 값이 숫자면 절대값으로 반환한다.
select abs(min(lat_n)-max(lat_n))+abs(min(long_w)-max(long_w))
from STATION
2️⃣ Round it to a scale of 4 decimal places
이제 위에서 구한 식에 출력 형태문만 지정하면 된다.
문제에서 소숫점 네 자리까지 표기하라 했으니 이를 그대로 round() 함수에 넣어주면 된다.
select round(abs(min(lat_n)-max(lat_n))+abs(min(long_w)-max(long_w)), 4)
from STATION
'SQL > HackerRank' 카테고리의 다른 글
[SQL] The PADS (0) | 2024.04.30 |
---|---|
[SQL] Weather Observation Station 5 (0) | 2024.04.24 |
[SQL] Weather Observation Station 3 (0) | 2024.03.12 |
[SQL] Revising the Select Query I (0) | 2024.03.11 |