경쟁 프로그래밍쉬움

웨이브 정렬

'Wave Sort' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

정수 배열 arr을 가져와 오름차순으로 정렬한 다음 인덱스 0부터 시작하여 모든 인접한 요소 쌍을 바꾸는(즉, arr[0]과 arr[1]을 바꾼 다음 arr[2]와 arr[3]을 바꾸는 등) arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4]... 속성을 만족하는 웨이브 정렬 배열을 생성하는 함수 wave_sort(arr)을 작성하세요. 결과 배열을 반환합니다.

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

Example 1
Input
wave_sort([3, 6, 5, 10, 7, 20])
Output
[5, 3, 7, 6, 20, 10]
Explanation

First, sort the array to get [3, 5, 6, 7, 10, 20]. Swapping adjacent pairs: swap 3 and 5 -> [5, 3...], swap 6 and 7 -> [..., 7, 6...], swap 10 and 20 -> [..., 20, 10]. Result is [5, 3, 7, 6, 20, 10].

Example 2
Input
wave_sort([10, 90, 49, 2, 1, 5, 23])
Output
[2, 1, 10, 5, 49, 23, 90]
Explanation

Sort array to [1, 2, 5, 10, 23, 49, 90]. Swapping adjacent pairs gives [2, 1, 10, 5, 49, 23, 90].

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

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