-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy path315-count-of-smaller-numbers-after-self.py
43 lines (39 loc) · 1.54 KB
/
315-count-of-smaller-numbers-after-self.py
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
32
33
34
35
36
37
38
39
40
41
42
43
class Solution:
def countSmaller(self, nums: List[int]) -> List[int]:
counts = [0 for _ in range(len(nums))]
nums = [(num, i) for i, num in enumerate(nums)]
def merge_sort(nums):
n = len(nums)
if n <= 1:
return nums
m = n // 2
left_part = merge_sort(nums[: m])
right_part = merge_sort(nums[m:])
res = []
left, right = 0, 0
while left < m or right < n - m:
if left == m:
res.append(right_part[right])
right += 1
elif right == n - m:
res.append(left_part[left])
counts[left_part[left][1]] += right
left += 1
elif left_part[left][0] <= right_part[right][0]:
res.append(left_part[left])
counts[left_part[left][1]] += right
left += 1
else:
res.append(right_part[right])
right += 1
return res
merge_sort(nums)
return counts
# time O(nlogn), each layer to merge cost O(n)
# space O(n), due to new list, merge sort has logn stack layers
# using array and sort and merge sort
'''
1. the count is updated for each element from the left part during merging
(think about how many elements from right part and place before cur element)
(means these elements are smaller than cur element and locate at cur elements right side before sorting)
'''