forked from DmrfCoder/AlgorithmAndDataStructure
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path28.py
62 lines (49 loc) · 1.49 KB
/
28.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
# -*- coding:utf-8 -*-
class Solution:
def siftUp(self, a, index):
while (True):
if index == 1:
return a
parentIndex = int(index / 2)
if a[parentIndex] < a[index]:
t = a[parentIndex]
a[parentIndex] = a[index]
a[index] = t
index = parentIndex
else:
return a
def siftDown(self, a, index):
size = len(a) - 1
while (True):
index = 2 * index
if index > size:
return a
if index + 1 <= size:
if a[index + 1] > a[index]:
index += 1
if a[index] > a[int(index / 2)]:
t = a[index]
a[index] = a[int(index / 2)]
a[int(index / 2)] = t
def makeHeap(self, a, k):
for i in range(int(k / 2), 0, -1):
a = self.siftDown(a, i)
return a
def GetLeastNumbers_Solution(self, tinput, k):
# write code here
if tinput is None:
return []
size = len(tinput)
if size < k or k==0:
return []
a = [0]
a = a + tinput[0:k]
a = self.makeHeap(a, k)
for i in range(k, len(tinput)):
if tinput[i] < a[1]:
a[1] = tinput[i]
a = self.siftDown(a, 1)
a = sorted(a[1:])
return a
s = Solution()
s.GetLeastNumbers_Solution([4, 5, 1, 6, 2, 7, 3, 8], 4)