Python 기본 사항쉬움

다른 배열에 따라 정렬

'다른 배열에 따라 정렬' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

arr2에 정의된 순서에 따라 arr1의 요소를 정렬하는 함수 sort_by_order(arr1, arr2)을 작성하세요. arr2에 나타나는 arr1의 요소는 arr2에 나타나는 순서대로 먼저 와야 합니다. arr2에 없는 요소는 정렬된(오름차순) 순서로 끝에 표시되어야 합니다.

제약
  • 1 <= len(arr1) <= 10^5
  • 0 <= len(arr2) <= 100
  • Elements of arr2 are distinct

Example 1
Input
arr1 = [2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8], arr2 = [2, 1, 8, 3]
Output
[2, 2, 1, 1, 8, 8, 3, 5, 6, 7, 9]
Explanation

First all 2s, then 1s, then 8s, then 3s (order from arr2). Remaining [5,6,7,9] sorted ascending.

Example 2
Input
arr1 = [4, 5, 6], arr2 = [6, 4]
Output
[6, 4, 5]
Explanation

6 first, then 4 (per arr2 order). 5 is not in arr2, goes at end.

Example 3
Input
arr1 = [1, 2, 3], arr2 = []
Output
[1, 2, 3]
Explanation

No order specified, so sort ascending.

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

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