LeetCode #1365. How Many Numbers Are Smaller Than the Current Number

题目

1365. How Many Numbers Are Smaller Than the Current Number


解题方法

设立一个temp数组存放nums中元素的有序序列,遍历temp中的每个元素i,累计已经遍历过的数的个数countnum,如果当前元素i不在dic中就加进去,值设置为已经遍历过的数的个数countnum,这个遍历结束之后遍历nums查字典写个数即可。
时间复杂度:O(nlogn)
空间复杂度:O(n)


代码

class Solution:
    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
        temp = sorted(nums)
        countnum = 0
        dic = {}
        for i in temp:
            if i not in dic:
                dic[i] = countnum
            countnum += 1
        
        rat = []
        for i in range(len(nums)):
            rat.append(dic[nums[i]])
        
        return rat
上一篇:CF1029A Many Equal Substrings


下一篇:Too many connections 以及设置最大连接数