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
32 changes: 32 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions Problem3.py
Original file line number Diff line number Diff line change
@@ -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