Skip to content
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
21 changes: 21 additions & 0 deletions find_a_peak.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
time - o(log(n))
space- o(1)
We perform binary search and we eliminate search space based on the condition that the peak element does not exist in the decreasing
part of the search space. If mid+1 is greater than mid then we eliminate the mid-1 and before section, if not eliminate mid+1
"""

from typing import List


class Solution:
def findPeakElement(self, nums: List[int]) -> int:
lo, hi = 0, len(nums) - 1
while lo <= hi:
mid = lo + (hi - lo) // 2
if mid == len(nums) - 1 or nums[mid] >= nums[mid + 1]:
hi = mid - 1
else:
lo = mid + 1

return lo
28 changes: 28 additions & 0 deletions find_min_rotated_sorted_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
time - o(log(n))
space - o(1)
We perform binary search to eliminate search space that is sorted because a sorted section of a rotated array does not contain the pivot
element (except with low and hi are in sorted order). there are do stopping conditions, we find mid and if mid is smaller than
left and right elements we return mid. If low and hi are sorted then it means low is the pivot and we return.
"""

from typing import List


class Solution:
def findMin(self, nums: List[int]) -> int:
lo, hi = 0, len(nums) - 1
while lo <= hi:
if nums[lo] <= nums[hi]:
return nums[lo]
mid = lo + (hi - lo) // 2
if (
0 < mid < len(nums) - 1
and nums[mid + 1] > nums[mid]
and nums[mid - 1] > nums[mid]
):
return nums[mid]
if nums[lo] <= nums[mid]:
lo = mid + 1
else:
hi = mid
51 changes: 51 additions & 0 deletions first_last_position_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
Time complexity - o(logn)
space complexity - o(1)
We find the first occurence doing binary search, we modify the exit condition, instead of return the index as soon as we find the target
like in normal binary search we enter into a sub condition where we check if the element to the left of the found target index is lesser
than target, if yes we found the first occurence, if no, there are more to the left so we reduce search space by making hi as mid-1
if we don't find the target in the initial search, return -1 and return [-1,-1] in the main function
if we do find the first occurence, we need to find the last occurence and we do that by again doing another binary search, this is done
by making first occurence of target as lo and end of list as hi. We once again check if mid==target and if it is, we enter into a sub-condition
if the number to the right of mid is greater than target then we return mid if not we move lo to mid+1
"""

from typing import List


class Solution:
def search_start_index(self, nums, target):
lo = 0
hi = len(nums) - 1
while lo <= hi:
mid = lo + (hi - lo) // 2
if nums[mid] == target:
if mid == 0 or nums[mid - 1] < target:
return mid
else:
hi = mid - 1
elif nums[mid] > target:
hi = mid - 1
else:
lo = mid + 1
return -1

def search_last_index(self, nums, target, lo, hi):
while lo <= hi:
mid = lo + (hi - lo) // 2
if nums[mid] == target:
if mid == len(nums) - 1 or nums[mid + 1] > target:
return mid
else:
lo = mid + 1
elif nums[mid] > target:
hi = mid - 1
else:
lo = mid + 1

def searchRange(self, nums: List[int], target: int) -> List[int]:
start_index = self.search_start_index(nums, target)
if start_index == -1:
return [-1, -1]
last_index = self.search_last_index(nums, target, start_index, len(nums) - 1)
return [start_index, last_index]