Python 기본 사항쉬움

각 요소를 순위로 교체

'순위별로 각 요소 바꾸기' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

배열이 오름차순으로 정렬될 때 배열의 각 요소를 순위로 바꾸는 함수 replace_by_rank(arr)을 작성하세요. 가장 작은 요소는 순위 1을 얻고 두 번째로 작은 요소는 순위 2를 얻는 식으로 진행됩니다. 두 요소가 동일하면 동일한 순위를 얻습니다. 순위 배열을 반환합니다.

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

Example 1
Input
arr = [20, 15, 26, 2, 98, 6]
Output
[4, 3, 5, 1, 6, 2]
Explanation

Sorted: [2,6,15,20,26,98]. Ranks: 2->1, 6->2, 15->3, 20->4, 26->5, 98->6.

Example 2
Input
arr = [10, 10, 10]
Output
[1, 1, 1]
Explanation

All elements are equal, so all get rank 1.

Example 3
Input
arr = [5, 3, 1]
Output
[3, 2, 1]
Explanation

Sorted: [1,3,5]. Ranks: 1->1, 3->2, 5->3.

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

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