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 Binary_Search-2 #1903

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
56 changes: 56 additions & 0 deletions FInd_first_and_last_position_of_element_in_sorted_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
if len(nums) == 0 or nums == None:
return [-1,-1]

first = self.BinarySearch_first(nums, target)
if first == -1:
return [-1,-1]

last = self.BinarySearch_last(nums, target)

return [first, last]

def BinarySearch_first(self, nums: List[int], target: int)-> int:
left = 0
right = len(nums) -1

while left <= right:
mid = left + (right - left) //2

if target == nums[mid]:
# Check if it is the first Occurence
if nums[mid -1] != target or mid == 0:
return mid
else:
# move to the left
right = mid -1

elif nums[mid] > target:
right = mid -1
else:
left = mid + 1

return -1

def BinarySearch_last(self, nums: List[int], target: int)-> int:
left = 0
right = len(nums) -1

while left <= right:
mid = left + (right - left) //2

if target == nums[mid]:
# Check if it is the last Occurence
if len(nums) -1 == mid or nums[mid+1] != target:
return mid
else:
# move to the right
left = mid +1

elif nums[mid] > target:
right = mid -1
else:
left = mid + 1

return -1
19 changes: 19 additions & 0 deletions FInd_minimum_in_rotated_sorted_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
def findMin(self, nums: List[int]) -> int:
if nums == None or len(nums) == 0:
return -1
low = 0
high = len(nums)-1
mid = low + (high -low)// 2
while low < high:
if nums[low] <= nums[high]:
return nums[low]
mid = low + (high-low) //2
if (mid == 0 or (nums[mid] < nums[mid - 1])) and (mid == len(nums)-1 or (nums[mid] < nums[mid + 1])):
return nums[mid]
# move to the unsorted part of the array
if nums[low] <= nums[mid]:
low = mid+1
else:
high = mid-1
return nums[high]
25 changes: 25 additions & 0 deletions FInd_peak_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution:
def findPeakElement(self, nums: List[int]) -> int:
if len(nums) == 0 or nums == None:
return -1

if len(nums) == 1:
return 0

left = 0
right = len(nums) -1


while left <= right:
mid= left + (right-left) //2

if nums[mid-1] < nums[mid] and nums[mid] > nums[mid+1]:
return mid
elif nums[mid] < nums[mid+1]:
left = mid +1
elif nums[mid] < nums[mid-1]:
right = mid- 1

return 1010