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

Comp Coding week 1 Done #1073

Open
wants to merge 3 commits 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
40 changes: 40 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# // Time Complexity : O(log(n)) because ninary search
# // Space Complexity : O(1)
# // Did this code successfully run on Leetcode : NA
# // Any problem you faced while coding this : No

# An array without duplicate elements. Find missing elemnt.
# We know that '1' element is missing. We need to eliminate one half of the array to use binary search.
# We can check the difference between values and compare it with difference between indexes
# If one side doesnt have he missing element, we know its on the other side. Rinse and repeat.
# [1, 2, 3,^5, 6, 7, 8] OG array
# [0, 1, 2, 3, 4, 5, 6] index
# Output = 4

class Solution:
def missingItem(self, arr:list) -> int:

if arr == []:
return "no elements"
if len(arr) == 1:
return "only 1 elemnt"

high = len(arr)-1
low = 0


while high-low>1:
mid = low +(high-low)//2
if arr[mid] - arr[low] != mid - low: # left of mid is missing
high = mid
else: # right of mid is missing
low = mid

return arr[low]+1


print(Solution().missingItem([1,2,3,5,6,7,8]))
print(Solution().missingItem([5,7]))
print(Solution().missingItem([1]))
print(Solution().missingItem([]))

36 changes: 36 additions & 0 deletions minHeapImplementation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import sys

class MinHeap:
def __init__(self, maxsize) -> None:
self.maxsize = maxsize
self.size = 0
self.Heap = [0]*(maxsize+1)
self.Heap[0] = -1 * sys.maxsize
self.FRONT = 1

def parent(self,pos):
return pos//2

def leftChild(self,pos):
return 2 * pos

def rightChild(self,pos):
return (2 * pos)+1

def size(self):
return -1 #incomplete video error

def peek(self):
return -1

def heapify(self):
return -1

def add(self):
return -1

def remove(self):
return -1

def print(self):
return -1