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
37 changes: 37 additions & 0 deletions problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# LEETCODE PROBLEM 34. FIND FIRST AND LAST POSITION OF ELEMENT IN SORTED ARRAY
# TIME COMPLEXITY: O(log N) where N is the number of elements in the array
# SPACE COMPLEXITY: O(1)
# Any problem you faced while coding this: Tried a new methdology on how can I avoid using
# two different function, I had some hiccups with the logic but later was successful

class Solution(object):
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""

def findTarget(nums,start,end,target,isFirst):
bestFound=-1
while start<=end:
mid=(start+end)//2
if nums[mid]<target:
start=mid+1
elif nums[mid]>target:
end=mid-1
else:
bestFound=mid
if isFirst:
end=mid-1
else:
start=mid+1
return bestFound

high=len(nums)-1
firstValue=findTarget(nums,0,high,target,True)
if firstValue==-1:
return[-1,-1]
secondValue=findTarget(nums,firstValue,high,target,False)
return [firstValue,secondValue]

20 changes: 20 additions & 0 deletions problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# LEETCODE PROBLEM 153. FIND MINIMUM IN ROTATED SORTED ARRAY
# TIME COMPLEXITY: O(log N) where N is the number of elements in the array
# SPACE COMPLEXITY: O(1)
# Any problem you faced while coding this: None

class Solution(object):
def findMin(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
low=0
high=len(nums)-1
while low<high:
mid=(low+high)//2
if nums[mid]<=nums[high]:
high=mid
else:
low=mid+1
return nums[low]
23 changes: 23 additions & 0 deletions problem3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# LEETCODE PROBLEM 162. FIND PEAK ELEMENT
# TIME COMPLEXITY: O(log N) where N is the number of elements in the array
# SPACE COMPLEXITY: O(1)
# Any problem you faced while coding this: Had a bit issue when I was making the edge cases
# ie the index 0 and index n-1 logic

class Solution(object):
def findPeakElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
low=0
high=len(nums)-1
while (low<=high):
mid=(low+high)//2
if (mid==0 or nums[mid-1]<nums[mid]) and (mid==len(nums)-1 or nums[mid]>nums[mid+1]):
return mid
elif (mid==len(nums)-1 or nums[mid+1]>nums[mid]):
low=mid+1
else:
high=mid-1
return -1