상위 150개 인터뷰쉬움

K 원점에 가장 가까운 지점

'원점에 가장 가까운 K 지점' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

points[i] = [xi, yi]가 X-Y 평면의 한 점과 정수 k를 나타내는 점 배열이 주어지면 원점(0, 0)에 가장 가까운 k개 점을 반환합니다.

X-Y 평면의 두 점 사이의 거리는 유클리드 거리(즉, sqrt((x1 - x2)^2 + (y1 - y2)^2))입니다.

어떤 순서로든 답변을 반환할 수 있습니다. 답변은 고유한 것으로 보장됩니다(순서 제외).

kClosest(points: List[List[int]], k: int) -> List[List[int]] 함수를 작성하세요.

제약
  • 1 <= k <= len(points) <= 10^4
  • -10^4 <= xi, yi <= 10^4

Example 1
Input
points = [[1,3],[-2,2]], k = 1
Output
[[-2,2]]
Explanation

The distance from (1, 3) to the origin is sqrt(10). The distance from (-2, 2) to the origin is sqrt(8). Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.

Example 2
Input
points = [[3,3],[5,-1],[-2,4]], k = 2
Output
[[3,3],[-2,4]]
Explanation

The closest two points are (3, 3) and (-2, 4). (Order of elements in the output does not matter).

Need a Hint?
세트나 힙과 같은 힙/우선순위 큐 관련 데이터 구조를 사용하는 것을 고려해보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.