Skip to content
Open

Solved: #2280

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
70 changes: 70 additions & 0 deletions FindFirstAndLastPositionOfElementInSortedArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'''
In this problem in order to find 2 indexes of the target element, I performed 2 binray search operations

1. firstBinarySearch: To find first occurance of the element.
for first occurance I compared mid with target and moved the pointer towards left if the mid element is equal. If not I moved high pointer to right.

If the first occurance is not found then the target does not exist in the array. So we return [-1, -1] as output

2. lastBinarySearch: To find last occurance of the element
for last occurance I made first occurance as the low pointer, and compared mid with target and moved the pointer towards right if the mid element is less than the target.

'''

class Solution:
def searchRange(self, nums, target):
# to find first occurance of the element
def firstBinarySearch(nums, target, low, high):
while low <= high:
mid = low + (high - low) // 2
# comparing mid with target
if nums[mid] == target:
# check if it is the first occurance or check mid-1 less than mid
if mid == 0 or nums[mid - 1] < nums[mid]:
# if any condition is true return mid
return mid
else:
# else move the high pointer to left
high = mid - 1
elif nums[mid] > target:
# move the pointer to left
high = mid - 1
else:
# move the pointer to right
low = mid + 1
return -1
# to find last occurance of the element
def lastBinarySearch(nums, target, low, high):
while low <= high:
mid = low + (high - low) // 2
# comparing mid with target
if nums[mid] == target:
# checking if mid is the last element or mid+1 elemeent is greater than mid element
if mid == len(nums) - 1 or nums[mid + 1] > nums[mid]:
return mid
else:
# moving the pointer to right
low = mid + 1
elif nums[mid] > target:
high = mid - 1
else:
low = mid + 1
return -1

first = firstBinarySearch(nums, target, 0, len(nums) - 1)
# if the first occarnec is -1 then the target does not exist in the array so we directly return [-1, -1]
if first == -1:
return [-1, -1]
# here we take the low pointer from the first occurance of the target
last = lastBinarySearch(nums, target, first, len(nums) - 1)
# array of frst occurance and last occurance
return [first, last]

'''
Time Complexity : O(log n)
Since the time complexity of performing binray serach is O(log n), for both binray searches: O(log n) + O(log n) = 2 O(log n) => O(log n)

Space Complexity: O(1)
We did not use any extra space.

'''
40 changes: 40 additions & 0 deletions FindMinimumInRotatedSortedArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'''
In this problem Iused binray search to find the element faster.
In a rotated sorted array at least one side of the array is always sorted.

We check if mis is the minimum element
We compare mid element with low and high pointer elements:
if low is less than mid, its left sorted
if mid is less than high, its right sorted

then we ignore the sorted side of the array and search of the other half, since that half has the minimum element

If the whole array is sorted then we directly return low element

'''
class Solution:
def findMin(self, nums: List[int]) -> int:
low = 0
high = len(nums)-1
while low <= high:
if nums[low] <= nums[high]:
return nums[low]
mid = (low+high)//2
# to check if mid element is the minimum element
if mid > 0 and nums[mid] < nums[mid-1]:
return nums[mid]
# comparing low element with mid element
elif nums[low] <= nums[mid]:
# moving the pointer to right
low = mid+1
else:
# moving the pointer to left
high = mid-1

'''
Time Complexity: O(log n)
since we are using binray search and binary search takes O(log n) time

Space Complexity: O(1)
we did not use any extra space.
'''
42 changes: 42 additions & 0 deletions FindPeakElement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'''
In this problem to find the peak element, we compare the mid with either of its neighbors and move our pointer towards higher neighbour direction
There will always be a peak element if we move our pointer towards the higher element direction.

In the first condition we check if mid itself is the peak:
by checking mid is the last elemnt or mid element is greater than its right neighbour
and mid the first elemnt or mid is greater than its left neighbour

If mid is not the peak element, we check which direction should we move the pointer to find a peak.
Since there will always be a peak element we would never reach the last return statement.
'''
class Solution:
def findPeakElement(self, nums: List[int]) -> int:
n = len(nums)
low = 0
high = n-1
while low <= high:
mid = (low+high)//2
# check if mid is our peek element or not
# (mid is not our last elemnt or mid greater than mid+1) and (mid is zero or mid is greater than mid-1)
if (mid == n-1 or nums[mid] > nums[mid+1]) and (mid is 0 or nums[mid] > nums[mid-1]):
# mid is peak
return mid
# check if mid-1 element is less than mid and mid is greater than zero
elif mid > 0 and nums[mid-1] > nums[mid]:
# we go left side of the array
high = mid - 1
else:
# we go to right side of the array
low = mid+1
# we will reach the peek elemnt for sure so we will not reach this return statement
return 123456

'''
Time Complexity : O(log n)
Since we are performing binray serach for finding the element

Space Complexity: O(1)
We did not use any extra space.

'''