-
Notifications
You must be signed in to change notification settings - Fork 8
/
rightSmallerThan.py
101 lines (89 loc) · 2.72 KB
/
rightSmallerThan.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
# O(N^(N/2)) time | O(N) space
# def rightSmallerThan(array):
# if len(array)==0:
# return []
# result = []
# for i in range(len(array)):
# count=0
# current = array[i]
# left = i+1
# right = len(array)-1
# while right>=left:
# if left==right :
# if current > array[left] and current > array[right]:
# count+=1
# left+=1
# right-=1
# continue
# if current > array[left]:
# count+=1
# if current > array[right]:
# count+=1
# left+=1
# right-=1
# result.append(count)
# return result
# O(N^2) | O(N)
def rightSmallerThan(array):
if len(array) == 0:
return []
result = []
for i in range(len(array)):
count = 0
for j in range(i+1, len(array)):
if array[i] > array[j]:
count += 1
result.append(count)
return result
class SpecialBST:
def __init__(self, value, idx, numSmallerAtInsertTime) -> None:
self.value = value
self.idx = idx
self.left = None
self.right = None
self.numSmallerAtInsertTime= numSmallerAtInsertTime
self.leftSubTreeSize=0
def insert(self, val, idx, numSmallerAtInsertTime):
if self.value < val:
numSmallerAtInsertTime+=self.leftSubTreeSize
if val > self.value:
numSmallerAtInsertTime+=1
if self.right is None:
self.right = SpecialBST(val,idx,numSmallerAtInsertTime)
else:
self.right.insert(val,idx,numSmallerAtInsertTime)
else:
self.leftSubTreeSize += 1
if self.left is None:
self.left = SpecialBST(val,idx,numSmallerAtInsertTime)
else:
self.left.insert(val, idx, numSmallerAtInsertTime)
def rightSmallerThan(array):
if len(array) == 0:
return []
lastIdx = len(array)-1
bst = SpecialBST(array[lastIdx], lastIdx, 0)
for idx in reversed(range(len(array)-1)):
bst.insert(array[idx], idx, 0)
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)
"""
8
/ \
5 11
/
-1
\
3
/ \
2 4
"""
print(rightSmallerThan([8, 5, 11, -1, 3, 4, 2]))
# print(rightSmallerThan([9, 8, 10, 11, 12, 13]))