문제
Compute the Euclidean distance between (x1, y1) and (x2, y2): the square root of (x2 − x1)² + (y2 − y1)².
Implement distance(x1, y1, x2, y2).
Input
x1: First point x. y1: First point y. x2: Second point x. y2: Second point y.
Output
Return the distance.
제약 조건
-1,000,000 ≤ each coordinate ≤ 1,000,000
예시
입력:
x1 = 0.0
y1 = 0.0
x2 = 3.0
y2 = 4.0
예상 출력:
expected = 5.0
입력:
double x1 = 0.0;
double y1 = 0.0;
double x2 = 3.0;
double y2 = 4.0;
예상 출력:
double expected = 5.0;
입력:
double x1 = 0.0;
double y1 = 0.0;
double x2 = 3.0;
double y2 = 4.0;
예상 출력:
double expected = 5.0;
설명
√(3² + 4²) = √25 = 5.
입력:
x1 = 1.0
y1 = 1.0
x2 = 1.0
y2 = 1.0
예상 출력:
expected = 0.0
입력:
double x1 = 1.0;
double y1 = 1.0;
double x2 = 1.0;
double y2 = 1.0;
예상 출력:
double expected = 0.0;
입력:
double x1 = 1.0;
double y1 = 1.0;
double x2 = 1.0;
double y2 = 1.0;
예상 출력:
double expected = 0.0;
설명
Both points are the same, so the distance is 0.
접근 방법
먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.
힌트 1
Compute the horizontal and vertical differences between the two points.
힌트 2
Square both differences, add them, then take the square root of the total.
힌트 3
Watch for negative coordinates, but the distance formula still works the same.
예상 복잡도
O(1) time, O(1) extra space
참고 자료
이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제약 조건을 기준으로 풀어 주세요.