-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.py
35 lines (35 loc) · 831 Bytes
/
Solution.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
#coding=utf-8
__author__ = 'xuxuan'
class Solution(object):
def countSmaller(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
l=len(nums)
if l==0:
return []
ans=[]
arrays=[]
for i in range(l-1,-1,-1):
a=self.findIndex(nums[i],arrays)
ans.append(a)
arrays[a:a]=[nums[i]]
ans.reverse()
return ans
def findIndex(self,num,arrays):
if len(arrays)==0 or num<=arrays[0]:
return 0
s=0
e=len(arrays)-1
while s+1<e:
m=(s+e)/2
if arrays[m]>=num:
e=m
else:
s=m
if arrays[s]>=num:
return s
if arrays[e]>=num:
return e
return e+1