-
Notifications
You must be signed in to change notification settings - Fork 234
/
right-smaller-than.py
103 lines (91 loc) · 2.89 KB
/
right-smaller-than.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
# RIGHT SMALLER THAN
# O(N^2) time and O(N) space
def rightSmallerThan(array):
# Write your code here.
output = []
for i, x in enumerate(array):
count, index = 0, i + 1
while index < len(array):
if array[index] < x:
count += 1
index += 1
output.append(count)
return output
# Average and Best: O(NlogN) time and O(N) space
# Worst: O(N^2) time and O(N) space
def rightSmallerThan(array):
# Write your code here.
if len(array) == 0:
return []
lastIdx = len(array) - 1
bst = SpecialBST(array[lastIdx], lastIdx, 0)
for i in reversed(range(len(array) - 1)):
bst.insert(array[i], i)
rightSmallerCounts = array[:]
getRightSmallerCounts(bst, rightSmallerCounts)
return rightSmallerCounts
def getRightSmallerCounts(bst, rightSmallerCounts):
if bst is None:
return
rightSmallerCounts[bst.idx] = bst.numSmallerAtInsertTime
getRightSmallerCounts(bst.left, rightSmallerCounts)
getRightSmallerCounts(bst.right, rightSmallerCounts)
class SpecialBST:
def __init__(self, value, idx, numSmallerAtInsertTime):
self.value = value
self.idx = idx
self.numSmallerAtInsertTime = numSmallerAtInsertTime
self.leftSubtreeSize = 0
self.left = None
self.right = None
def insert(self, value, idx, numSmallerAtInsertTime=0):
if value < self.value:
self.leftSubtreeSize += 1
if self.left is None:
self.left = SpecialBST(value, idx, numSmallerAtInsertTime)
else:
self.left.insert(value, idx, numSmallerAtInsertTime)
else:
numSmallerAtInsertTime += self.leftSubtreeSize
if value > self.value:
numSmallerAtInsertTime += 1
if self.right is None:
self.right = SpecialBST(value, idx, numSmallerAtInsertTime)
else:
self.right.insert(value, idx, numSmallerAtInsertTime)
# Average and Best: O(NlogN) time and O(N) space
# Worst: O(N^2) time and O(N) space
def rightSmallerThan(array):
# Write your code here.
if len(array) == 0:
return []
lastIdx = len(array) - 1
rightSmallerCounts = array[:]
bst = SpecialBST(array[lastIdx])
rightSmallerCounts[lastIdx] = 0
for i in reversed(range(len(array) - 1)):
bst.insert(array[i], i, rightSmallerCounts)
return rightSmallerCounts
class SpecialBST:
def __init__(self, value):
self.value = value
self.leftSubtreeSize = 0
self.left = None
self.right = None
def insert(self, value, idx, rightSmallerCounts, numSmallerAtInsertTime=0):
if value < self.value:
self.leftSubtreeSize += 1
if self.left is None:
self.left = SpecialBST(value)
rightSmallerCounts[idx] = numSmallerAtInsertTime
else:
self.left.insert(value, idx, rightSmallerCounts, numSmallerAtInsertTime)
else:
numSmallerAtInsertTime += self.leftSubtreeSize
if value > self.value:
numSmallerAtInsertTime += 1
if self.right is None:
self.right = SpecialBST(value)
rightSmallerCounts[idx] = numSmallerAtInsertTime
else:
self.right.insert(value, idx, rightSmallerCounts, numSmallerAtInsertTime)