From f1ae2d84bef17a03ad4f292d771b95d9ba33eeeb Mon Sep 17 00:00:00 2001 From: LEGEND0WSKI Date: Fri, 25 Oct 2024 22:35:24 -0500 Subject: [PATCH 1/3] Minimum Rotated 1/3 Done --- findFirstLastSorted.py | 7 +++++++ findMinRotatedSorted.py | 46 +++++++++++++++++++++++++++++++++++++++++ findPeakElement.py | 7 +++++++ 3 files changed, 60 insertions(+) create mode 100644 findFirstLastSorted.py create mode 100644 findMinRotatedSorted.py create mode 100644 findPeakElement.py diff --git a/findFirstLastSorted.py b/findFirstLastSorted.py new file mode 100644 index 00000000..5569c816 --- /dev/null +++ b/findFirstLastSorted.py @@ -0,0 +1,7 @@ +#// Time Complexity : +#// Space Complexity : +#// Did this code successfully run on Leetcode : +#// Any problem you faced while coding this : + + +#// Your code here along with comments explaining your approach in three sentences only diff --git a/findMinRotatedSorted.py b/findMinRotatedSorted.py new file mode 100644 index 00000000..19a2fe6b --- /dev/null +++ b/findMinRotatedSorted.py @@ -0,0 +1,46 @@ +#// Time Complexity : O(log(n)) +#// Space Complexity : O(1) +#// Did this code successfully run on Leetcode : Yes +#// Any problem you faced while coding this : Edge(0,n-1) condition is confusing. + + +#// Your code here along with comments explaining your approach in three sentences only + +# TO find smallest term in a rotated array we can use binary search. +# but first we el;iminate the conditions we can use to make it run faster. (no rotation array) +# We can check if neighbours are smaller that m or not in the binary search. m-1 and m-1 (Alternative m-1 , m) +class Solution: + def findMin(self, nums: List[int]) -> int: + n = len(nums) + low = 0 + high = n-1 + + while low <= high: + + if nums[low] < nums[high]: #No rotation array + return nums[low] + #For rotated array we take mid for binary search + mid = low + (high - low)//2 + if (mid == 0 or nums[mid-1] > nums[mid]) and (mid == n-1 or nums[mid+1] > nums[mid]): #eleminate neighbours, -1 and n conditions + return nums[mid] + elif nums[high] < nums[mid]: #check right. Left sorted + low = mid + 1 + else: + high = mid -1 + + return -1 + +# Eliminate edges and neighbour condition +# class Solution: +# def findMin(self, nums: List[int]) -> int: +# n = len(nums) +# low = 0 +# high = n-1 + +# while low < high: #Simple Binary +# mid = low + (high - low)//2 +# if nums[mid] > nums[high] : #right sorted +# low = mid + 1 +# else: +# high = mid # boss condition +# return nums[low] \ No newline at end of file diff --git a/findPeakElement.py b/findPeakElement.py new file mode 100644 index 00000000..5569c816 --- /dev/null +++ b/findPeakElement.py @@ -0,0 +1,7 @@ +#// Time Complexity : +#// Space Complexity : +#// Did this code successfully run on Leetcode : +#// Any problem you faced while coding this : + + +#// Your code here along with comments explaining your approach in three sentences only From 12f74837ed6307e75553635c6c21117da52aa9c0 Mon Sep 17 00:00:00 2001 From: LEGEND0WSKI Date: Fri, 25 Oct 2024 23:15:38 -0500 Subject: [PATCH 2/3] 2/3 Done --- findPeakElement.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/findPeakElement.py b/findPeakElement.py index 5569c816..71971a45 100644 --- a/findPeakElement.py +++ b/findPeakElement.py @@ -1,7 +1,27 @@ -#// Time Complexity : -#// Space Complexity : -#// Did this code successfully run on Leetcode : -#// Any problem you faced while coding this : +#// Time Complexity : O(log(n)) +#// Space Complexity : O(1) +#// Did this code successfully run on Leetcode : Yes +#// Any problem you faced while coding this : No #// Your code here along with comments explaining your approach in three sentences only +# We try to satisfy 3 conditions : neighbours, outliers: binary search +# Find mid point then check neighbours +# Tried to implement a condition which checks 2nd and 2nd-last condition but it is already satisfied in neighbour case. + + +class Solution: + def findPeakElement(self, nums: List[int]) -> int: + n = len(nums) + l = 0 + h = n-1 + + while l <= h: + mid = l+(h-l)//2 + if (mid == 0 or nums[mid-1] < nums[mid]) and (mid == n-1 or nums[mid+1] < nums[mid]): # neighbour case also eliminates (-inf,-inf) + return mid + elif nums[mid] < nums[mid+1]: # simple binary search + l = mid +1 + else: + h = mid + return -1 \ No newline at end of file From 41c281d62d0b5f298328301adab0332eb64e7274 Mon Sep 17 00:00:00 2001 From: LEGEND0WSKI Date: Sat, 26 Oct 2024 13:40:26 -0500 Subject: [PATCH 3/3] 3/3 Done --- findFirstLastSorted.py | 66 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/findFirstLastSorted.py b/findFirstLastSorted.py index 5569c816..bdd85238 100644 --- a/findFirstLastSorted.py +++ b/findFirstLastSorted.py @@ -1,7 +1,65 @@ -#// Time Complexity : -#// Space Complexity : -#// Did this code successfully run on Leetcode : -#// Any problem you faced while coding this : +#// Time Complexity : O(log(n)): log(m) + log(n) since we are using binary search twice +#// Space Complexity : O(1) : No aux space used +#// Did this code successfully run on Leetcode : Yes +#// Any problem you faced while coding this : Not much. Felt Code is too lengthy. Maybe reduce to 1 function. #// Your code here along with comments explaining your approach in three sentences only +# We do 2 binary searches to find the first and last element. +# we also need to consider if it is an empty array +# also make sure if target is in array or not. + + +class Solution: + def searchRange(self, nums: list[int], target: int) -> list[int]: + + if len(nums) == 0: # empty array + return [-1,-1] + first = self.binarySearchFirst(nums, target, 0,len(nums)-1) # element not in array + if first == -1: + return [-1,-1] + + last = self.binarySearchLast(nums, target, 0, len(nums)-1) # no need to check twice + return [first,last] + + def binarySearchFirst(self, nums: list[int], target, low, high): # binary search for first term + while low <= high: + + mid = low + (high-low)//2 # find mid + if nums[mid] == target: #if mid is target + if (mid == 0 or nums[mid] != nums[mid-1]): # check if left of mid is not eq to mid + return mid + else: + high = mid -1 + elif nums[mid] < target: + low = mid + 1 + else: + high = mid - 1 + return -1 + + def binarySearchLast(self, nums: list[int], target, low , high): # binary search for last term + while low <= high: + + mid = low + (high-low)//2 # find mid + if nums[mid] == target: # if mid is target + if (mid == high or nums[mid] != nums[mid+1]): # check if right of mid is not eq to mid + return mid + else: + low = mid + 1 + elif nums[mid] > target: + high = mid - 1 + else: + low = mid + 1 + return -1 + + +#testing +arr = [5,6,6,6,7,7,8,9,9] +target = 6 +x = Solution().searchRange(arr,target) +print(x) + + + + +