diff --git a/leetcode153.py b/leetcode153.py new file mode 100644 index 00000000..5eb01b44 --- /dev/null +++ b/leetcode153.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Sun Feb 8 15:37:19 2026 + +@author: rishigoswamy + + Problem: + ---------- + https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ + + Approach: + ---------- + The array is originally sorted in ascending order and then rotated at + an unknown pivot, which causes the minimum element to be the rotation + point. + + If the array is already sorted (nums[low] < nums[high]), the first + element is the minimum and can be returned immediately. + + Otherwise, we use binary search to locate the pivot by checking the + relationship between nums[mid] and its neighboring elements. + If nums[mid] is smaller than nums[mid - 1], it is the minimum; + if nums[mid + 1] is smaller than nums[mid], then nums[mid + 1] is the + minimum. + + Based on whether nums[mid] lies in the left sorted portion or the right + rotated portion, we discard one half of the search space and continue. + + Time Complexity: + ---------------- + O(log n), where n is the number of elements in the array. + + Space Complexity: + ----------------- + O(1), since the search is performed in-place using constant extra space. + + Did this code successfully run on Leetcode: + ------------------------------------------ + Yes + + Any problem you faced while coding this: + --------------------------------------- + Carefully handling boundary conditions when accessing nums[mid - 1] + and nums[mid + 1] to avoid index out-of-range errors. + +""" + + +class Solution: + def findMin(self, nums) -> int: + low = 0 + high = len(nums) - 1 + + if low == high: + return nums[0] + + if nums[low] < nums[high]: + return nums[low] + + while low <= high: + mid = (low + high)//2 + + if nums[mid] < nums[mid-1]: + return nums[mid] + if nums[mid+1] < nums[mid]: + return nums[mid+1] + + if nums[mid] > nums[low]: + low = mid + 1 + else: + high = mid - 1 + diff --git a/leetcode162.py b/leetcode162.py new file mode 100644 index 00000000..b67d64ff --- /dev/null +++ b/leetcode162.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Sun Feb 8 15:43:49 2026 + +@author: rishigoswamy + + Problem: + ---------- + https://leetcode.com/problems/find-peak-element/ + + Approach: + ---------- + A peak element is defined as an element that is strictly greater than + its neighboring elements, and the problem guarantees that a peak + always exists. + + We use binary search to efficiently find a peak by comparing the + middle element with its neighbors. + If the middle element is greater than both neighbors, it is a peak + and can be returned immediately. + + If the left neighbor is greater than the middle element, a peak must + exist in the left half of the array; otherwise, a peak must exist in + the right half. + By discarding half of the search space at each step, we maintain + logarithmic time complexity. + + Special boundary checks are performed for the first and last elements + to safely handle edge cases. + + Time Complexity: + ---------------- + O(log n), where n is the number of elements in the array. + + Space Complexity: + ----------------- + O(1), since the search is performed in-place using constant extra space. + + Did this code successfully run on Leetcode: + ------------------------------------------ + Yes + + Any problem you faced while coding this: + --------------------------------------- + Handling boundary conditions for the first and last indices and + correctly deciding which half of the array to discard during the + binary search. + + +""" +import List + +class Solution: + def findPeakElement(self, nums: List[int]) -> int: + low = 0 + high = len(nums) - 1 + + if high == low: + return 0 + + while low <= high: + + mid = (low+high)//2 + + if mid == 0 and nums[mid+1] < nums[mid]: + return mid + + elif mid == len(nums) - 1 and nums[mid-1] < nums[mid]: + return mid + + elif nums[mid-1] < nums[mid] and nums[mid+1] < nums[mid]: + return mid + + if mid > 0 and nums[mid] < nums[mid-1]: + high = mid - 1 + + elif mid < len(nums): + low = mid + 1 + + diff --git a/leetcode34.py b/leetcode34.py new file mode 100644 index 00000000..b3f6052e --- /dev/null +++ b/leetcode34.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Sun Feb 8 15:39:30 2026 + +@author: rishigoswamy + + Problem: + ---------- + https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/ + + Approach: + ---------- + Since the array is sorted in ascending order, binary search can be used + to locate the first and last occurrences of the target efficiently. + + We perform two separate binary searches: + - The first search finds the leftmost (first) position of the target by + continuing the search on the left half even after the target is found. + - The second search finds the rightmost (last) position of the target by + continuing the search on the right half after the target is found. + + Each search carefully checks boundary conditions to ensure the correct + extreme index is identified. + + Time Complexity: + ---------------- + First binary search : O(log n) + Second binary search : O(log n) + Overall : O(log n) + + Space Complexity: + ----------------- + O(1), since only constant extra space is used. + + Did this code successfully run on Leetcode: + ------------------------------------------ + Yes + + Any problem you faced while coding this: + --------------------------------------- + Handling edge cases where the target appears at the beginning or end + of the array, and ensuring both searches terminate correctly when the + target does not exist. + +""" + +class Solution: + def searchRange(self, nums, target): + low = 0 + high = len(nums) - 1 + mid = (low + high)//2 + first, second = -1, -1 + + while low <= high: + mid = (low + high)//2 + if nums[mid] == target: + if mid == 0 or nums[mid-1] != nums[mid]: + first = mid + break + else: + high = mid - 1 + elif nums[mid] > target: + high = mid - 1 + else: + low = mid + 1 + + + low = 0 + high = len(nums) - 1 + while low <= high: + mid = (low + high)//2 + if nums[mid] == target: + if mid == len(nums)-1 or nums[mid+1] != target: + second = mid + break + else: + low = mid + 1 + elif nums[mid] > target: + high = mid - 1 + else: + low = mid + 1 + + return [first, second] \ No newline at end of file