-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcount-of-smaller-numbers-after-self.py
56 lines (47 loc) · 1.68 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
class Solution:
# using a custom merge sort algorithm
def countSmaller(self, nums: List[int]) -> List[int]:
def divide(l, h, nums, res):
if l >= h:
return
m = l + (h - l) // 2
divide(l, m, nums, res)
divide(m+1, h, nums, res)
i, j = l, m+1
while i <= m:
while j <= h and nums[j][1] < nums[i][1]: j += 1
res[nums[i][0]] += max(j - m - 1, 0)
i += 1
merge(l, m, h, nums)
def merge(l, m, h, nums):
temp = nums[l: m+1]
i, j, k = 0, m+1, l
while i < len(temp) and j <= h:
if temp[i][1] <= nums[j][1]:
nums[k] = temp[i]
i += 1
else:
nums[k] = nums[j]
j += 1
k += 1
while i < len(temp):
nums[k] = temp[i]
i, k = i+1, k+1
while j <= h:
nums[k] = nums[j]
j, k = j+1, k+1
nums = [(i, el) for i, el in enumerate(nums)]
res = [0]*len(nums)
divide(0, len(nums)-1, nums, res)
return res
from sortedcontainers import SortedList
class Solution2:
# using a SortedList from sortedcontainers
def countSmaller(self, nums: List[int]) -> List[int]:
sl = SortedList()
res = []
for i in range(len(nums)-1, -1, -1):
idx = sl.bisect_left(nums[i])
sl.add(nums[i])
res.append(idx)
return list(reversed(res))