diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..bc126736 --- /dev/null +++ b/Problem1.py @@ -0,0 +1,32 @@ +# https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/description/ + +# This problem demonstrates finding first and last occurence of an element in a sorted array. The input array will have elements in sorted manner, but there will be repetitions. The implementation can be done by doing 2 binary searches, one for finding first position and another to find last position. + +class Solution: + def searchRange(self, nums: List[int], target: int) -> List[int]: + left = self.binSearch(nums, target, True) + right = self.binSearch(nums, target, False) + return [left, right] + + def binSearch(self, nums, target, leftBias): + low, high = 0, len(nums) - 1 + + while low <= high: + mid = (low + high) // 2 + if nums[mid] > target: + high = mid - 1 + elif nums[mid] < target: + low = mid + 1 + else: + if leftBias: + if mid == low or nums[mid - 1] != nums[mid]: + return mid + else: + high = mid - 1 + else: + if mid == high or nums[mid + 1] != nums[mid]: + return mid + else: + low = mid + 1 + + return -1 \ No newline at end of file diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..0dff4391 --- /dev/null +++ b/Problem2.py @@ -0,0 +1,21 @@ +# https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ + +# This problem is about finding the minimum element in a rotated sorted array. By applying the binary search approach, this solution runs in O(log n) time complexity. The binary search approach eliminates the half of the array by trying to find sorted part in the array. The exit condition is when both the neighbors of mid element are greater then the mid itself. +class Solution: + def findMin(self, nums: List[int]) -> int: + low, high = 0, len(nums) - 1 + + while low <= high: + mid = (low + high) // 2 + + #return the first element if the array is not rotated + if nums[low] <= nums[high]: + return nums[low] + if (mid == low or nums[mid] < nums[mid - 1]) and (mid == high or nums[mid] < nums[mid + 1]): + return nums[mid] + if nums[mid] >= nums[low]: #left sorted + low = mid + 1 + else: + high = mid - 1 #right sorted + + return -1 \ No newline at end of file diff --git a/Problem3.py b/Problem3.py new file mode 100644 index 00000000..fa74c81e --- /dev/null +++ b/Problem3.py @@ -0,0 +1,21 @@ +# https://leetcode.com/problems/find-peak-element/ + +# This problem is about finding the peak element in an array. The array will be unsorted or sorted. No two adjacent elements will be the same, but there can be repetitive elements. Goal is to find the index of any peak element. A peak element will be greater then it's neighbor elements. + +class Solution: + def findPeakElement(self, nums: List[int]) -> int: + low, high = 0, len(nums) - 1 + + if len(nums) == 1: + return 0 + + while low <= high: + mid = (low + high) // 2 + if mid == high or nums[mid] < nums[mid + 1]: + low = mid + 1 + elif mid == low or nums[mid] < nums[mid - 1]: + high = mid - 1 + if (mid == low or nums[mid] > nums[mid - 1]) and (mid == high or nums[mid] > nums[mid + 1]): + return mid + + return -1 \ No newline at end of file