Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 프로그래머스
- dacon
- 백준
- 우분투
- PYTHON
- leetcode
- Kaggle
- programmers
- 데이콘
- AI 경진대회
- 더현대서울 맛집
- 편스토랑 우승상품
- 맥북
- gs25
- ubuntu
- 자연어처리
- Real or Not? NLP with Disaster Tweets
- 금융문자분석경진대회
- 캐치카페
- SW Expert Academy
- 프로그래머스 파이썬
- github
- Git
- Docker
- 코로나19
- ChatGPT
- 파이썬
- Baekjoon
- 편스토랑
- hackerrank
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 1331. Rank Transform of an Array (Python) 본문
728x90
반응형
Given an array of integers arr, replace each element with its rank.
The rank represents how large the element is. The rank has the following rules:
- Rank is an integer starting from 1.
- The larger the element, the larger the rank. If two elements are equal, their rank must be the same.
- Rank should be as small as possible.
Example 1:
Input: arr = [40,10,20,30]
Output: [4,1,2,3]
Explanation: 40 is the largest element. 10 is the smallest. 20 is the second smallest. 30 is the third smallest.
Example 2:
Input: arr = [100,100,100]
Output: [1,1,1]
Explanation: Same elements share the same rank.
Example 3:
Input: arr = [37,12,28,9,100,56,80,5,12]
Output: [5,3,4,2,8,6,7,1,3]
Constraints:
- 0 <= arr.length <= 105
- -109 <= arr[i] <= 109
Solution
class Solution:
def arrayRankTransform(self, arr: List[int]) -> List[int]:
arr_temp = list(set(arr))
sorted_list= sorted(arr_temp)
rank_dict = {}
for i, num in enumerate(sorted_list):
rank_dict[num] = i + 1
answer = [rank_dict[num] for num in arr]
return answer
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 28. Implement strStr() (Python) (0) | 2020.09.10 |
---|---|
[leetCode] 1337. The K Weakest Rows in a Matrix (Python) (0) | 2020.09.09 |
[leetCode] 506. Relative Ranks (Python) (0) | 2020.09.07 |
[leetCode] 1507. Reformat Date (Python) (0) | 2020.09.06 |
[leetCode] 415. Add Strings (Python) (0) | 2020.09.05 |
Comments