diff --git a/First:Last_Position_SortedArray.py b/First:Last_Position_SortedArray.py new file mode 100644 index 00000000..00e450b3 --- /dev/null +++ b/First:Last_Position_SortedArray.py @@ -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] diff --git a/Minimum.py b/Minimum.py new file mode 100644 index 00000000..27cbd210 --- /dev/null +++ b/Minimum.py @@ -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] diff --git a/Peak_element.py b/Peak_element.py new file mode 100644 index 00000000..31326e57 --- /dev/null +++ b/Peak_element.py @@ -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