forked from Lazy-Pig/CodingInterviewChinese2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path40_最小的k个数.py
74 lines (55 loc) · 1.78 KB
/
40_最小的k个数.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
import random
"""
解法一:时间复杂度为O(n)的算法,只有当我们可以修改输入的数组时可用
"""
def partitaion(array, start, end):
if not isinstance(array, list) or not isinstance(start, int) or not isinstance(end, int) or \
start < 0 or end > len(array) - 1:
return
pivot = random.randint(start, end)
array[pivot], array[start] = array[start], array[pivot]
left = start + 1
right = end
while True:
while left <= right and array[left] < array[start]:
left += 1
while left <= right and array[right] > array[start]:
right -= 1
if left > right:
break
array[left], array[right] = array[right], array[left]
array[start], array[right] = array[right], array[start]
return right
def get_least_numbers1(array, k):
if not isinstance(array, list) or len(array) == 0 or not isinstance(k, int) or k <= 0 or k > len(array):
return
start = 0
end = len(array) - 1
index = partitaion(array, start, end)
while index is not None and index != k - 1:
if index > k - 1:
end = index - 1
else:
start = index + 1
index = partitaion(array, start, end)
if index is None:
return
for i in range(k):
print(array[i], end=" ")
print()
"""
解法二:时间复杂度为O(nlogk)的算法, 特别适合处理海量数据
(不改变原数组)
"""
def get_least_numbers2(array, k):
import heapq
max_heap = []
for x in array:
heapq.heappush(max_heap, -x)
if len(max_heap) > k:
heapq.heappop(max_heap)
for x in max_heap:
print(-x, end=" ")
print()
if __name__ == "__main__":
get_least_numbers1([4, 5, 1, 6, 2, 7, 3, 8], 4)