Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Done mock 1 #1059

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions design_min_heap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class MinHeap:
def __init__(self):

self.heap = []

def getMin(self):

if not self.heap:
return None # If heap is empty
return self.heap[0]

def extractMin(self):

if len(self.heap) == 0:
return None
if len(self.heap) == 1:
return self.heap.pop()

# Swap the root with the last element and remove the last element
root = self.heap[0]
self.heap[0] = self.heap.pop()
self.minHeapify(0) # Restore the heap property
return root

def insert(self, key):
"""
Inserts a new key into the heap.
Time Complexity: O(log N)
"""
self.heap.append(key) # Add the new key at the end
index = len(self.heap) - 1
self.bubbleUp(index) # Move up to maintain heap property

def minHeapify(self, i):
"""
Helper function to maintain the heap property.
Time Complexity: O(log N)
"""
smallest = i
left = 2 * i + 1
right = 2 * i + 2

if left < len(self.heap) and self.heap[left] < self.heap[smallest]:
smallest = left
if right < len(self.heap) and self.heap[right] < self.heap[smallest]:
smallest = right

if smallest != i:
# Swap and continue heapifying
self.heap[i], self.heap[smallest] = self.heap[smallest], self.heap[i]
self._minHeapify(smallest)

def bubbleUp(self, index):
parent = (index - 1) // 2
while index > 0 and self.heap[parent] > self.heap[index]:
# Swap with parent and update index
self.heap[index], self.heap[parent] = self.heap[parent], self.heap[index]
index = parent
parent = (index - 1) // 2
9 changes: 9 additions & 0 deletions missing_element_bs_mock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def search(arr):
l,r = 0, len(arr)-1
while l<=r:
mid = l+(r-l)//2
if arr[mid]== mid+1:
l= mid+1
else:
r = mid-1
return l+1