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
50 changes: 50 additions & 0 deletions FirstLastRotatedArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 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
# Approach : It performs two modified binary searches: one to find the first occurrence and one to find the last occurrence of the target.
# Each search narrows the range by checking neighbors when a match is found, ensuring the correct boundary index is returned.

class Solution:
def binarySearchFirst(self, nums: List[int], target: int, low: int, high: int) -> int:
while low <= high:
mid = low + ((high - low) // 2)

if nums[mid] == target:
if mid == low or nums[mid] != nums[mid - 1]:
return mid
else:
high = mid - 1
elif nums[mid] > target:
high = mid - 1
else:
low = mid + 1
return -1

def binarySearchLast(self, nums: List[int], target: int, low: int, high: int) -> int:
while low <= high:
mid = low + ((high - low) // 2)

if nums[mid] == target:
if mid == high or nums[mid] != nums[mid + 1]:
return mid
else:
low = mid + 1
elif nums[mid] > target:
high = mid - 1
else:
low = mid + 1
return -1

def searchRange(self, nums: List[int], target: int) -> List[int]:
if len(nums) == 0 or target < nums[0] or target > nums[len(nums)-1]:
return [-1, -1]

first = self.binarySearchFirst(nums, target, 0, len(nums) - 1)

if first == -1:
return [-1, -1]

last = self.binarySearchLast(nums, target, first, len(nums) - 1)

return [first, last]
24 changes: 24 additions & 0 deletions MinRotatedArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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
# Approach : It uses binary search to locate the smallest element in a rotated sorted array
# by checking neighbors. At each step, it discards the sorted half
# since the minimum must lie in the unsorted side.

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

while low <= high:
if nums[low] <= nums[high]: # only one element in range
return nums[low]

mid = low + (high - low) // 2

if (mid == low or nums[mid] < nums[mid - 1]) and (mid == high or nums[mid] < nums[mid + 1]):
return nums[mid]
elif nums[low] <= nums[mid]: # left sorted
low = mid + 1 # min always in unsorted side
else:
high = mid - 1
21 changes: 21 additions & 0 deletions PeakElement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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
# Approach : It uses binary search to compare each middle element with its neighbors
# and move toward the side that must contain a peak.
# If the middle element is greater than both neighbors, it returns that index as a peak.

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

while low <= high:
mid = low + (high - low) // 2

if (low == mid or nums[mid] > nums[mid - 1]) and (high == mid or nums[mid] > nums[mid + 1]):
return mid
elif mid == low or nums[mid] > nums[mid-1]:
low = mid + 1
else:
high = mid - 1