forked from super30admin/Competitive-Coding-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem-2.py
More file actions
68 lines (58 loc) · 1.92 KB
/
Problem-2.py
File metadata and controls
68 lines (58 loc) · 1.92 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
57
58
59
60
61
62
63
64
65
66
67
68
# Min Heap
# https://www.geeksforgeeks.org/min-heap-in-java/
class MinHeap:
def __init__(self):
self.l = 0
self.heap = [-1000]
def insert(self,item):
# Time Complexity: O(log n)
self.heap.append(item)
self.l += 1
i = self.l
# Heapify
while self.heap[i] < self.heap[i//2] and i > 1:
self.heap[i], self.heap[i//2] = self.heap[i//2], self.heap[i]
i = i//2
def extractMin(self):
# Time Complexity: O(log n)
temp = self.heap[1]
self.heap[1] = self.heap[self.l]
self.l -= 1
self.heap.pop()
i = 1
left_child = (2*i)+1
right_child = (2*i)+2
while self.heap[i] > self.heap[left_child] or self.heap[i] > self.heap[right_child]:
if self.heap[i] > self.heap[left_child] and self.heap[i] > self.heap[right_child]:
if self.heap[left_child] < self.heap[right_child]:
self.heap[i], self.heap[left_child] = self.heap[left_child], self.heap[i]
else:
self.heap[i], self.heap[right_child] = self.heap[right_child], self.heap[i]
elif self.heap[i] > self.heap[left_child]:
self.heap[i], self.heap[left_child] = self.heap[left_child], self.heap[i]
else:
self.heap[i], self.heap[right_child] = self.heap[right_child], self.heap[i]
return temp
def getMin(self):
# Time Complexity: O(1)
if not self.isEmpty():
return self.heap[1]
def isEmpty(self):
# Time Complexity: O(1)
if len(self.heap) == 1 and self.heap[0] == -1000:
return True
return False
obj = MinHeap()
print(obj.isEmpty())
obj.insert(5)
obj.insert(4)
obj.insert(14)
obj.insert(40)
obj.insert(1)
obj.insert(2)
obj.insert(6)
print(obj.isEmpty())
print(obj.getMin())
print(obj.extractMin())
print(obj.extractMin())
print(obj.getMin())