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
33 changes: 33 additions & 0 deletions 10. Find first and last in sorted array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Defining a class Solution to solve the problem of finding the starting and ending position of a target value in a sorted array.
class Solution:
# Defining the main function searchRange which is taking a sorted list nums and a target value.
# Calling BiSer twice: once to find the leftmost index and once to find the rightmost index of the target.
def searchRange(self, nums: List[int], target: int) -> List[int]:
left = self.BiSer(nums, target, True) # Searching for the leftmost index
right = self.BiSer(nums, target, False) # Searching for the rightmost index
return [left, right]

# Defining a helper function BiSer to perform binary search with a bias (left or right).
# Using binary search to efficiently find the target index.
# If leftBias is True, moving towards the left to find the first occurrence.
# If leftBias is False, moving towards the right to find the last occurrence.
def BiSer(self, nums, target, leftBias):
l = 0
r = len(nums) - 1
i = -1
while l <= r:
m = (l+r) // 2
if target > nums[m]:
l = m + 1
elif target < nums[m]:
r = m - 1
else:
i = m
if leftBias:
r = m - 1
else:
l = m + 1
return i

# Time Complexity (TC): O(log n), since binary search is being performed twice.
# Space Complexity (SC): O(1), since only a constant amount of extra space is being used.
21 changes: 21 additions & 0 deletions 11. Min in Rotated sorterd array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def findMin(self, nums: List[int]) -> int:
# Initializing left and right pointers
l = 0
r = len(nums) - 1
# Iterating while left pointer is less than right pointer
while l < r:
# Calculating middle index
m = l + (r - l) // 2
# Checking if middle element is greater than rightmost element
if nums[m] > nums[r]:
# Moving left pointer to mid + 1 since minimum is in right half
l = m + 1
else:
# Moving right pointer to mid since minimum could be at mid or in left half
r = m
# Returning the minimum element found
return nums[l]

# Time Complexity (TC): O(log n) - performing binary search
# Space Complexity (SC): O(1) - using constant extra space
22 changes: 22 additions & 0 deletions 12. Find peak element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution:
def findPeakElement(self, nums: List[int]) -> int:
# Initializing left and right pointers for binary search
l = 0
r = len(nums) - 1

# Performing binary search to find a peak element
while l <= r:
# Calculating the middle index
m = l + ((r-l) //2)
# Checking if the left neighbor is greater, so moving search to the left half
if m > 0 and nums[m] < nums[m-1]:
r = m - 1
# Checking if the right neighbor is greater, so moving search to the right half
elif m < len(nums) - 1 and nums[m] < nums[m + 1]:
l = m + 1
# If neither neighbor is greater, returning current index as peak
else:
return m

# Time Complexity (TC): O(log n), since binary search is being used.
# Space Complexity (SC): O(1), since only constant extra space is being used.