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 #1909

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
44 changes: 44 additions & 0 deletions First:Last_Position_SortedArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Approach:
# We perform binary search twice: first to find the leftmost index (first occurrence)
# and second to find the rightmost index (last occurrence) of the target.
# If the target is not present, both searches will return -1. This approach guarantees
# O(log n) time complexity by utilizing binary search instead of linear traversal.

# 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

from typing import List

class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
def findLeft(nums, target):
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] < target:
left = mid + 1
else:
right = mid - 1 # Continue searching left part
return left

def findRight(nums, target):
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] <= target:
left = mid + 1 # Continue searching right part
else:
right = mid - 1
return right

# Find the leftmost and rightmost positions
left_index = findLeft(nums, target)
right_index = findRight(nums, target)

# Check if the target is not found
if left_index <= right_index and right_index < len(nums) and nums[left_index] == target:
return [left_index, right_index]
else:
return [-1, -1]
29 changes: 29 additions & 0 deletions Minimum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Approach:
# We use binary search to find the minimum element in a rotated sorted array.
# The key is to compare the middle element with the right boundary; if mid > right,
# the minimum is in the right half; otherwise, it's in the left half.
# This allows us to locate the minimum efficiently in O(log n) time.

# 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

from typing import List

class Solution:
def findMin(self, nums: List[int]) -> int:
left, right = 0, len(nums) - 1

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

# If mid element is greater than the right element, the min is in the right half.
if nums[mid] > nums[right]:
left = mid + 1
# If mid element is smaller or equal to the right, min is in the left half (including mid).
else:
right = mid

# At the end of the loop, left == right pointing to the minimum element.
return nums[left]
29 changes: 29 additions & 0 deletions Peak_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Approach:
# We use a binary search to find a peak element. At each step, we compare the middle element with its right neighbor;
# if the middle element is smaller, a peak must exist on the right half, otherwise, it exists on the left half.
# This approach guarantees an O(log n) runtime by dividing the search space in half at each step.

# 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

from typing import List

class Solution:
def findPeakElement(self, nums: List[int]) -> int:
left, right = 0, len(nums) - 1

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

# If the middle element is smaller than the next element,
# a peak exists in the right half.
if nums[mid] < nums[mid + 1]:
left = mid + 1
# Otherwise, a peak exists in the left half (including mid).
else:
right = mid

# At the end of the loop, left == right and it points to a peak element.
return left