Skip to content

Commit c8af01f

Browse files
committed
topK 复杂度
1 parent f02d6c2 commit c8af01f

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

docs/15_堆与堆排序/heap_and_heapsort.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def test_heapsort_reverse():
121121
assert heapsort_reverse(l) == sorted(l, reverse=True)
122122
```
123123

124-
# python 里的 heapq
124+
# Python 里的 heapq 模块
125125
python 其实自带了 heapq 模块,用来实现堆的相关操作,原理是类似的。请你阅读相关文档并使用内置的 heapq 模块完成堆排序。
126126
一般我们刷题或者写业务代码的时候,使用这个内置的 heapq 模块就够用了。
127127

@@ -168,7 +168,7 @@ class TopK:
168168

169169
def test():
170170
import random
171-
i = list(range(1000))
171+
i = list(range(1000)) # 这里可以是一个可迭代元素,节省内存
172172
random.shuffle(i)
173173
_ = TopK(i, 10)
174174
print(_.get_topk())
@@ -185,6 +185,7 @@ if __name__ == '__main__':
185185
- 请你实现一个最小堆,你需要修改那些代码呢?
186186
- 我们实现的堆排序是 inplace 的吗,如果不是,你能改成 inplace 的吗?
187187
- 堆排序的时间复杂度是多少? siftup 和 siftdown 的时间复杂度是多少?(小提示:考虑树的高度,它决定了操作次数)
188+
- 请你思考 Top K 问题的时间复杂度是多少?
188189

189190

190191
# 延伸阅读

0 commit comments

Comments
 (0)