모든 챌린지
쉬움 14명 해결 정답률 50.0%

Distance Between Points

Compute the straight-line distance between two points.

배우게 될 내용

  • Apply the distance formula
  • Use square roots
  • Return a floating-point result

문제

번역본 보기

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

예시

예시 1
입력:
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.

예시 2
입력:
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에 맞게 수정되었습니다. 위의 설명과 제약 조건을 기준으로 풀어 주세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

# 코더 언어 시간 메모리 AI 평가 풀이
1 LMLaura Mahlow Python 676 ms - 5/5 코드 보기
2 KKKaneki Ken Python 677 ms - 5/5 코드 보기
3 ННикита Python 678 ms - 5/5 코드 보기
4 ゼゼロダニ Python 700 ms - 5/5 코드 보기
5 RCRiccardo Corti Python 705 ms - 5/5 코드 보기
6 JVJulien VACHAUDEZ Python 711 ms - 5/5 코드 보기
7 MSMichael Stoddart Python 712 ms - 5/5 코드 보기
8 HJhettory Jurash Python 732 ms - 5/5 코드 보기
9 LMliam morales Python 733 ms - 5/5 코드 보기
10 JPJittipat Phonhan Go 945 ms - 5/5 코드 보기
11 AAnjh3l0 C++ 2,565 ms - 5/5 코드 보기
12 ·𝓕·.★·.·⍣·.·★ 𝓕𝓪𝓫𝓲𝓸𝓵𝓪 𝓔𝓵𝓮𝓷𝓪 ★·.·⍣·.·★.· C++ 2,619 ms - 4/5 코드 보기
13 YSYoo Seungmin Java 2,626 ms - 5/5 코드 보기
14 PTphúc thịnh phạm C++ 2,672 ms - 4/5 코드 보기

다른 사람의 풀이 보기

커뮤니티 풀이

스포일러: 아래에 전체 풀이 14개가 있습니다 먼저 직접 풀어보세요. 다른 개발자의 코드와 AI 리뷰도 참고할 수 있습니다. 로그인하고 문제를 풀면 토론에 참여할 수 있습니다.