From 0b6fdeedc8660f611ad615fd269418acfc56480c Mon Sep 17 00:00:00 2001 From: LEGEND0WSKI Date: Mon, 28 Oct 2024 09:50:14 -0700 Subject: [PATCH 1/3] added base code --- test.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 test.py diff --git a/test.py b/test.py new file mode 100644 index 0000000..e30e90f --- /dev/null +++ b/test.py @@ -0,0 +1,15 @@ +arr = [1,2,3,5,6,7,8] +# [11,12,13,15,16,17,18] +# [0, 1, 2, 3, 4, 5, 6] +# Output = 4 +h = len(arr)-1 +l = 0 + +while h-l>1: + mid = l +(h-l)//2 #3 index, + if arr[mid] - arr[l] != mid - l: #left missing + h = mid + else: + l = mid + +print(arr[l]+1) From 7ca07025b5bfe8908fb7253c2d43465d4ebe517a Mon Sep 17 00:00:00 2001 From: LEGEND0WSKI Date: Mon, 28 Oct 2024 11:02:17 -0700 Subject: [PATCH 2/3] Find Missing Element --- Problem1.py | 25 +++++++++++++++++++++++++ test.py | 15 --------------- 2 files changed, 25 insertions(+), 15 deletions(-) create mode 100644 Problem1.py delete mode 100644 test.py diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 0000000..aa0f359 --- /dev/null +++ b/Problem1.py @@ -0,0 +1,25 @@ +# An array without duplicate elements. Find missing elemnt. +# Bruteforce is iterative search difference between index and element. +# We use Binary search to find index difference between low and highs with middle element. +# [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: + + 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 + +arr = [1,2,3,5,6,7,8] +x = Solution().missingItem(arr) +print(x) \ No newline at end of file diff --git a/test.py b/test.py deleted file mode 100644 index e30e90f..0000000 --- a/test.py +++ /dev/null @@ -1,15 +0,0 @@ -arr = [1,2,3,5,6,7,8] -# [11,12,13,15,16,17,18] -# [0, 1, 2, 3, 4, 5, 6] -# Output = 4 -h = len(arr)-1 -l = 0 - -while h-l>1: - mid = l +(h-l)//2 #3 index, - if arr[mid] - arr[l] != mid - l: #left missing - h = mid - else: - l = mid - -print(arr[l]+1) From f8ce207c0af587ac03b536f95d8a6f6273e5a77a Mon Sep 17 00:00:00 2001 From: LEGEND0WSKI Date: Thu, 31 Oct 2024 16:11:12 -0700 Subject: [PATCH 3/3] MissingTrue MinHeapFalse --- Problem1.py | 29 ++++++++++++++++++++++------- minHeapImplementation.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 minHeapImplementation.py diff --git a/Problem1.py b/Problem1.py index aa0f359..ac3484c 100644 --- a/Problem1.py +++ b/Problem1.py @@ -1,16 +1,28 @@ +# // 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. -# Bruteforce is iterative search difference between index and element. -# We use Binary search to find index difference between low and highs with middle element. -# [1, 2, 3, 5, 6, 7, 8] OG array +# 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 @@ -20,6 +32,9 @@ def missingItem(self, arr:list) -> int: return arr[low]+1 -arr = [1,2,3,5,6,7,8] -x = Solution().missingItem(arr) -print(x) \ No newline at end of file + +print(Solution().missingItem([1,2,3,5,6,7,8])) +print(Solution().missingItem([5,7])) +print(Solution().missingItem([1])) +print(Solution().missingItem([])) + diff --git a/minHeapImplementation.py b/minHeapImplementation.py new file mode 100644 index 0000000..836666c --- /dev/null +++ b/minHeapImplementation.py @@ -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 \ No newline at end of file