Skip to content

Commit ed53008

Browse files
committed
Time: 3 ms (71.95%), Space: 18 MB (26.48%) - LeetHub
1 parent 530c31f commit ed53008

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# time complexity: O(nlogn)
2+
# space complexity: O(n)
3+
from collections import defaultdict
4+
from typing import List
5+
6+
7+
class Solution:
8+
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
9+
freq = defaultdict(int)
10+
for num in nums:
11+
freq[num] += 1
12+
freqList = [(key, count) for key, count in freq.items()]
13+
freqList.sort()
14+
currCount = 0
15+
16+
prefixFreq = defaultdict(int)
17+
for key, count in freqList:
18+
prefixFreq[key] = currCount
19+
currCount += count
20+
21+
for i, num in enumerate(nums):
22+
nums[i] = prefixFreq[num]
23+
24+
return nums
25+
26+
27+
'''
28+
29+
[(1, 0), (2, 1), (3, 3), (8, 4)]
30+
[(1, 1), (2, 2), (3, 1), (8, 1)]
31+
32+
'''
33+
34+
nums = [8, 1, 2, 2, 3]
35+
print(Solution().smallerNumbersThanCurrent(nums))
36+
nums = [6, 5, 4, 8]
37+
print(Solution().smallerNumbersThanCurrent(nums))
38+
nums = [7, 7, 7, 7]
39+
print(Solution().smallerNumbersThanCurrent(nums))

0 commit comments

Comments
 (0)