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
34 changes: 34 additions & 0 deletions find_minimum_in_rotated_sorted_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/

# Using the binary search finding the minimum number. In the first condition check the low is less than high if yes we go the minimum number. In 'and' case check the mid is minimum than previous and next element if this condition satisfies then return it. Else check the array is left or righ sorted and move the pointer accordingly. Time Complexity O(log n)

class Solution:
def findMin(self, nums : list[int]) -> int:
low = 0
high = len(nums) - 1
while low <= high:
# first check is array sorted, if yes, we got the minimum number
if nums[low] <= nums[high]:
return nums[low]
# find the mid point
mid = (low + high) // 2
# this edge case check the mid is smaller than the previous and smaller than the next, if yes, we got the minimum number
if (mid == 0 or nums[mid] < nums[mid - 1]) and (mid == high - 1 or nums[mid]< nums[mid+1]):
return nums[mid]
# this condition means left half is sorted
elif nums[low] <= nums[mid]:
# the element is not in the left side move the pointer to right.
low = mid + 1
# when the left is not sorted so move the pointer to the left part
else:
high = mid - 1
return nums[low]

sh = Solution()
print(sh.findMin([3,4,5,1,2]))
print(sh.findMin([4,5,6,7,0,1,2]))
print(sh.findMin([11,13,15,17]))




27 changes: 27 additions & 0 deletions find_peak_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# https://leetcode.com/problems/find-peak-element/

# Using binary search finding the greater element than its neighbors. First check the mid element is greater than its neighbor by applying the 'and' condition. It this condition satisfies, return the mid_index. Else check which sides element is greater than the mid and move the pointer accordingly. Time Complexity O(log n)



class Solution:
def findPeakElement(self, nums:list[int]) -> int:
low = 0
high = len(nums)-1
while low <= high:
mid_index = (low + high) // 2
# this edge case check the mid is greater than the previous and smaller than the next, if yes, we got the peak element
if (mid_index == 0 or nums[mid_index] > nums[mid_index - 1]) and (mid_index == high - 1 or nums[mid_index] > nums[mid_index + 1]):
return mid_index
# if the right element is greater than the mid then move the search in right half
elif nums[mid_index + 1] > nums[mid_index]:
low = mid_index + 1
# if the left element is greater than the mid then move the search in left half
else:
high = mid_index - 1

sh = Solution()
print(sh.findPeakElement([1,2,3,1]))
print(sh.findPeakElement([1,2,1,3,5,6,4]))

# output = 2
66 changes: 66 additions & 0 deletions first_and_last_position_of_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/


# Using the binary search to find the first and last position of the target element in the array. Created two methods, first is to find the first occurance of the target and second is to find the last occurance. Passing the result of first method to second method as an argument, becuase we got the first occurance from that index we can find the last occurance.
# Time Complexity O(log n)

class Solution:
# method to find the first position of the element
def searchRange(self, nums: list[int], target: int) -> list[int]:
def binarySearchFirst(nums, target, low, high):
while low <= high:
mid = (low + high) // 2
# when the mid is target element
if nums[mid] == target:
# if the previous element of the mid is not a target or target is at zero position means we got the first occurance
if mid == 0 or nums[mid-1] != target:
return mid
# but if the mid - 1 is also target element then change the high pointer to calculate the first position of the element
else:
high = mid -1
# search the target in the left half
elif nums[mid] > target:
high = mid - 1
# search the target in the right half
else:
low = mid + 1
return -1

# method to find the last position of the element
def binarySearchLast(nums, target, low, high):
while low <= high:
mid = (low + high) // 2
# when the mid is target element
if nums[mid] == target:
# if the next element of the mid is not a target or target is at last position means we got the last occurance
if mid == len(nums)-1 or nums[mid + 1] != target:
return mid
else:
# but if the mid + 1 is also target element then change the lower pointer to calculate the last position of the element
low = mid + 1
elif nums[mid] > target:
# search the target in the left half
high = mid -1
else:
# search the target in the right half
low = mid + 1
return -1

# find the first position of the element by calling binarySearchFirst method and store into the variable 'first'
first = binarySearchFirst(nums, target, 0 , len(nums)-1 )
# if we get the -1 output means target is not present
if first == -1:
# exit the program
return [-1, -1]
else:
# if the target is present in the list then pass it's first occurance index value as the 'first' argument in the binarySearchLast method
last = binarySearchLast(nums, target, first, len(nums)-1)
return [first, last]




sh = Solution()
print(sh.searchRange([5,7,7,8,8,10],8))
print(sh.searchRange([5,7,7,8,8,10],7))
print(sh.searchRange([],0))