코딩테스트/SQL 코드카타

MySQL 해커랭크 | Weather Observation Station (맨하탄 거리, 유클리디언 거리)

ANNASENA 2024. 8. 19. 08:00
728x90

 

맨하탄 거리 문제

https://www.hackerrank.com/challenges/weather-observation-station-18/problem?isFullScreen=true

 

Weather Observation Station 18 | HackerRank

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

www.hackerrank.com

  • Query the  Manhattan Distance between points P1 and P2
    • a = MIN(LAT_N)
    • b = MIN(LONG_W)
    • c = MAX(LAT_N)
    • d = MAX(LONG_W)
  • round it to a scale of 4 decimal places

 

테이블

STATION 테이블

 

풀이과정

▶ 맨하탄 거리(Manhattan Distance)

$ d = ∣p1−q1∣+∣p2−q2∣ $
 

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

 

# 정답

  • 수평 거리 : 위도(Latitude)끼리 뺀 후 절댓값 처리 해주기
  • 수직 거리 : 경도(Longtitude)끼리 뺀 후 절댓값 처리 해주기
1
2
SELECT ROUND(ABS(MIN(LAT_N)-MAX(LAT_N)) + ABS(MIN(LONG_W)-MAX(LONG_W)),4)
FROM STATION
cs

 


 

유클리드 거리 문제

https://www.hackerrank.com/challenges/weather-observation-station-19/problem?isFullScreen=true

 

Weather Observation Station 19 | HackerRank

Query the Euclidean Distance between two points and round to 4 decimal digits.

www.hackerrank.com

 

  • Query the Euclidean Distance between points P1 and P2
    • a = MIN(LAT_N)
    • b = MAX(LAT_N)
    • c = MIN(LONG_W)
    • d = MAX(LONG_W)
  • format your answer to display 4 decimal digits

 

풀이과정

▶ 유클리디언 거리(Euclidean_distance)

 

Euclidean distance - Wikipedia

From Wikipedia, the free encyclopedia Length of a line segment Using the Pythagorean theorem to compute two-dimensional Euclidean distance In mathematics, the Euclidean distance between two points in Euclidean space is the length of the line segment betwee

en.wikipedia.org

 

# 정답

  • 수평 거리( b - a ) : 위도(Latitude)끼리 뺀 후 제곱하기
  • 수직 거리( d - c ) : 경도(Longtitude)끼리 뺀 후 제곱하기
  • 수평거리와 수직거리의 제곱을 더한 후 제곱근 도출하기
1
2
SELECT ROUND(SQRT(POWER(MAX(LAT_N)-MIN(LAT_N), 2+ POWER(MAX(LONG_W)- MIN(LONG_W),2)),4)
FROM STATION
cs

728x90