forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 14
/
count-of-smaller-numbers-after-self.py
142 lines (124 loc) · 4.38 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# Time: O(nlogn)
# Space: O(n)
class Solution(object):
def countSmaller(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
def countAndMergeSort(num_idxs, start, end, counts):
if end - start <= 0: # The size of range [start, end] less than 2 is always with count 0.
return 0
mid = start + (end - start) / 2
countAndMergeSort(num_idxs, start, mid, counts)
countAndMergeSort(num_idxs, mid + 1, end, counts)
r = mid + 1
tmp = []
for i in xrange(start, mid + 1):
# Merge the two sorted arrays into tmp.
while r <= end and num_idxs[r][0] < num_idxs[i][0]:
tmp.append(num_idxs[r])
r += 1
tmp.append(num_idxs[i])
counts[num_idxs[i][1]] += r - (mid + 1)
# Copy tmp back to num_idxs
num_idxs[start:start+len(tmp)] = tmp
num_idxs = []
counts = [0] * len(nums)
for i, num in enumerate(nums):
num_idxs.append((num, i))
countAndMergeSort(num_idxs, 0, len(num_idxs) - 1, counts)
return counts
# Time: O(nlogn)
# Space: O(n)
# BIT solution.
class Solution2(object):
def countSmaller(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
class BIT(object): # 0-indexed.
def __init__(self, n):
self.__bit = [0]*(n+1) # Extra one for dummy node.
def add(self, i, val):
i += 1 # Extra one for dummy node.
while i < len(self.__bit):
self.__bit[i] += val
i += (i & -i)
def query(self, i):
i += 1 # Extra one for dummy node.
ret = 0
while i > 0:
ret += self.__bit[i]
i -= (i & -i)
return ret
# Get the place (position in the ascending order) of each number.
sorted_nums = sorted(zip(nums, range(len(nums))))
lookup = {i:new_i for new_i, (_, i) in enumerate(sorted_nums)}
# Count the smaller elements after the number.
result, bit = [0]*len(nums), BIT(len(nums))
for i in reversed(xrange(len(nums))):
result[i] = bit.query(lookup[i]-1)
bit.add(lookup[i], 1)
return result
# Time: O(nlogn) ~ O(n^2)
# Space: O(n)
# BST solution.
class Solution3(object):
def countSmaller(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
res = [0] * len(nums)
bst = self.BST()
# Insert into BST and get left count.
for i in reversed(xrange(len(nums))):
bst.insertNode(nums[i])
res[i] = bst.query(nums[i])
return res
class BST(object):
class BSTreeNode(object):
def __init__(self, val):
self.val = val
self.count = 0
self.left = self.right = None
def __init__(self):
self.root = None
# Insert node into BST.
def insertNode(self, val):
node = self.BSTreeNode(val)
if not self.root:
self.root = node
return
curr = self.root
while curr:
# Insert left if smaller.
if node.val < curr.val:
curr.count += 1 # Increase the number of left children.
if curr.left:
curr = curr.left
else:
curr.left = node
break
else: # Insert right if larger or equal.
if curr.right:
curr = curr.right
else:
curr.right = node
break
# Query the smaller count of the value.
def query(self, val):
count = 0
curr = self.root
while curr:
# Insert left.
if val < curr.val:
curr = curr.left
elif val > curr.val:
count += 1 + curr.count # Count the number of the smaller nodes.
curr = curr.right
else: # Equal.
return count + curr.count
return 0