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

completed Competitive-Coding-1 #1131

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
15 changes: 15 additions & 0 deletions Find-Missing-Number-in-a-sorted-array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# tc O(log n), sc O(1).
start, end = 0, len(nums) - 1

while start <= end:
mid = start + ((end - start) // 2)

# find missing numbers upto mid index
missing_nums = nums[mid] - nums[0] - mid
if missing_nums < k:
start = mid + 1
else:
end = mid - 1

missing_nums_upto_end = nums[end] - nums[0] - end
return nums[end] + (k - missing_nums_upto_end)
54 changes: 54 additions & 0 deletions Min-Heap-Implementation-using-Array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# tc O(log n), for insert, extract_min, heapify_up, heapify_down, O(1) for get_min, size, is_empty,
# sc O(n).
class MinHeap:
def __init__(self):
self.heap = []

def parent(self, idx):
return (idx-1) // 2

def left_child(self, idx):
return 2 * idx + 1

def right_child(self, idx):
return 2 * idx + 2

def insert(self, item):
self.heap.append(item)
self._heapify_up(len(self.heap)-1)

def extract_min(self):
if not self.heap:
return None
if len(self.heap) == 1:
return self.heap.pop()
self.heap[0] = self.heap.pop()
self._heapify_down(0)

def _heapify_up(self, idx):
while idx > 0 and self.heap[idx] < self.heap[self.parent(idx)]:
self.heap[idx], self.heap[self.parent(idx)] = self.heap[self.parent(idx)], self.heap[idx]
idx = self.parent(idx)

def _heapify_down(self,index):
smallest = index
left = self.left_child(index)
right = self.right_child(index)

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 != index:
self.heap[index], self.heap[smallest] = self.heap[smallest], self.heap[index]
self._heapify_down(smallest)

def get_min(self):
return self.heap[0] if self.heap else None

def size(self):
return len(self.heap)

def is_empty(self):
return not self.heap