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 |
29 | 30 | 31 |
Tags
- 캐치카페
- Kaggle
- 더현대서울 맛집
- 우분투
- Docker
- 편스토랑
- gs25
- 금융문자분석경진대회
- 맥북
- dacon
- leetcode
- 프로그래머스
- 코로나19
- github
- ubuntu
- 프로그래머스 파이썬
- programmers
- 백준
- PYTHON
- Git
- ChatGPT
- 데이콘
- 자연어처리
- SW Expert Academy
- Baekjoon
- AI 경진대회
- 파이썬
- hackerrank
- 편스토랑 우승상품
- Real or Not? NLP with Disaster Tweets
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