aboutsummaryrefslogtreecommitdiff
path: root/rank_transform_array.py
blob: 5f6f860a4a3b256a2aadf314703fa04e3f84577c (plain)
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
# 1331. Rank Transform of an Array

"""
given an array of integers 'arr', replace each element with its rank. the
rank reprsents 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.
"""


class Solution(object):
    def arrayRankTransform(self, arr):
        """
        :type arr: List[int]
        :rtype: List[int]
        """
        rank = {}
        for i in sorted(arr):
            rank.setdefault(i, len(rank) + 1)
        return map(rank.get, arr)


if __name__ == "__main__":
    obj = Solution()
    print(obj.arrayRankTransform(arr=[40, 10, 20, 30]))
    print(obj.arrayRankTransform(arr=[100, 100, 100]))
    print(obj.arrayRankTransform(arr=[37, 12, 28, 9, 100, 56, 80, 5, 12]))