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

BinarySearch-2 Completed #1911

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
65 changes: 65 additions & 0 deletions findFirstLastSorted.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#// 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)





46 changes: 46 additions & 0 deletions findMinRotatedSorted.py
Original file line number Diff line number Diff line change
@@ -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]
27 changes: 27 additions & 0 deletions findPeakElement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#// 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