-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountSmallerNumbers.py
More file actions
56 lines (53 loc) · 1.73 KB
/
CountSmallerNumbers.py
File metadata and controls
56 lines (53 loc) · 1.73 KB
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
# 315. Count of Smaller Numbers After Self
class SearchTreeNode:
# 构建二叉搜索树,其中count用来记录小于val的点的个数
def __init__(self, x):
self.val = x
self.count = 0
self.left = None
self.right = None
class Solution:
# 二分查找,数组从后往前遍历,将遍历到的数加入到新的数组,新的数组实现排序,用二分插入法将新元素插入,然后返回它的
# index-1即为对应的数量
def countSmaller(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
if not nums:
return []
if len(nums) == 1:
return [0]
counts = [0] * len(nums)
sorting = [nums[-1]]
for i in reversed(range(len(counts) - 1)):
curr = nums[i]
if curr > sorting[-1]:
sorting.append(curr)
counts[i] = len(sorting) - 1
else:
lo, hi = 0, len(sorting) - 1
while lo < hi:
mid = (lo + hi) // 2
if sorting[mid] < curr:
lo = mid + 1
elif sorting[mid] >= curr:
hi = mid
sorting.insert(hi, curr)
counts[i] = hi
return counts
class Solution2:
# 利用bisect维持有序数组
def countSmaller(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
import bisect
sorting = []
counts = []
for num in reversed(nums):
index = bisect.bisect_left(sorting, num)
sorting.insert(index, num)
counts.append(index)
return list(reversed(counts))