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
67 changes: 67 additions & 0 deletions Problem_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class Solution:
'''
This function helps in finding the first occurance of the target.
Once we land on one of the occurance of target, we check if this is the first by looking at the number before it.

Otherwise we eliminate the right side of the mid as the first occurance will be on the left for sure.
Remember to make sure the if statements are clearly defined so that there is not a default case that can alter l or r.
'''
def searchFirst(self, nums, target):
l = 0
r = len(nums) - 1

while l <= r:
m = l + (r-l)//2

if nums[m] == target:
if m == 0 or nums[m-1] != nums[m]:
return m
else:
r = m-1

if nums[m] > target:
r = m-1
if nums[m] < target:
l = m+1

return -1

'''
Since we already found the first occurance we now proceed to find the last occurance of the target in the
right of the first occurance including the first occurance since it can be possible that there is only one occurance of target.

If we come across the target, we check if it is the last occurance, otherwise we look on the right of the mid,
since the last occurance will definately be on the right.
'''
def searchLast(self, nums, first, target):
l = first
r = len(nums) - 1

while l <= r:
m = l + (r-l)//2

if nums[m] == target:
if m == len(nums)-1 or nums[m+1] != nums[m]:
return m
else:
l = m+1

if nums[m] > target:
r = m-1
if nums[m] < target:
l = m+1

return first

'''
We start by finding the first occurance of the target, if found we proceed to find the last occurance of the target.
If first occurance is not found that means target is not in the array and hence return [-1, -1]
'''
def searchRange(self, nums: List[int], target: int) -> List[int]:
first = self.searchFirst(nums, target)
if first == -1:
return [-1, -1]

last = self.searchLast(nums, first, target)

return [first, last]
29 changes: 29 additions & 0 deletions Problem_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'''
We use the binary search, and then the idea is that the min will always be in the unsorted array

In case both the sides of the mid turns out to be sorted:

0 1 2

then we ignore the right side since the mid will always be on the left in such case.

We check if the mid is not at the edge and check if the neighboring numbers are bigger than mid
then we found our min number in the rotated sorted array.
'''

class Solution:
def findMin(self, nums: List[int]) -> int:
l = 0
r = len(nums) - 1

while l <= r:
m = l + (r-l)//2
if (m == 0 or nums[m-1] > nums[m]) and (m == len(nums)-1 or nums[m+1] > nums[m]):
return nums[m]

if nums[m] < nums[r]:
r = m-1
else:
l = m+1

return -1
26 changes: 26 additions & 0 deletions Problem_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'''
We apply binary search strategy.

On mid we check if it is eligible to be peak. If not we continue to the next step.

We want to follow the uphill in the array beacuse going uphill will guarentee a peak.
Check which side is going uphill and eliminate the other side.
'''

class Solution:
def findPeakElement(self, nums: List[int]) -> int:
l = 0
r = len(nums) - 1

while l <= r:
m = l + (r-l)//2

if (m == 0 or nums[m-1] < nums[m]) and (m == len(nums)-1 or nums[m+1] < nums[m]):
return m

if m != 0 and nums[m-1] > nums[m]:
r = m-1
else:
l = m+1

return -1