경쟁 프로그래밍쉬움

가장 가까운 쌍

'가장 가까운 쌍' 문제에 대한 자세한 가이드 및 Python 구현.

문제 설명

쉬움

정렬되지 않은 정수 배열 arr을 가져와 최소 절대 차이가 있는 배열에서 두 정수 (x, y)의 튜플을 반환하는 함수 closest_pair(arr)을 작성하세요. 반환된 튜플은 x <= y과 같이 정렬되어야 합니다. 최소 차이가 동일한 쌍이 여러 개 있는 경우 x 값이 더 작은 쌍을 반환합니다(동일한 경우 y 값이 더 작음).

제약
  • 2 <= len(arr) <= 10^5
  • -10^9 <= arr[i] <= 10^9

Example 1
Input
closest_pair([4, 9, 1, 32, 13])
Output
(1, 4)
Explanation

The difference between 1 and 4 is 3, which is the minimum difference possible between any two elements in the array.

Example 2
Input
closest_pair([12, 15, 17, 20, 25])
Output
(15, 17)
Explanation

The difference between 15 and 17 is 2, which is the minimum difference possible.

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 리소스

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