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
16 changes: 16 additions & 0 deletions FindPeakElement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def findPeakElement(self, nums: List[int]) -> int:
l=0
h=len(nums)-1

while l<=h:
m=(l+h)//2

if( m==0 or nums[m]>nums[m-1] )and (m==len(nums)-1 or nums[m]>nums[m+1]):
return m
elif nums[m+1]>nums[m]:
l=m+1
else:
h=m-1

return 89434334
45 changes: 45 additions & 0 deletions First & last postion of element in sorted array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:

first=self.binarySearchFirst(nums, target)
if first ==-1:
return [-1,-1]
last=self.binarySearchLast(nums,target)
if last==-1:
return[-1,-1]
return [first,last]

def binarySearchFirst(self, nums: List[int], target: int) -> List[int]:
l=0
h=len(nums)-1

while l<=h:
m=(l+h)//2

if target==nums[m]:
if m==0 or nums[m-1]<nums[m]:
return m
else:
h=m-1
elif target>=nums[l] and target<nums[m]:
h=m-1
else:
l=m+1
return -1
def binarySearchLast(self, nums: List[int], target: int) -> List[int]:
l=0
h=len(nums)-1

while l<=h:
m=(l+h)//2

if target==nums[m]:
if m==len(nums)-1 or nums[m]<nums[m+1]:
return m
else:
l=m+1
elif target>=nums[l] and target<nums[m]:
h=m-1
else:
l=m+1
return -1
18 changes: 18 additions & 0 deletions find Min element in rotated sorted array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution:
def findMin(self, nums: List[int]) -> int:

l=0
h=len(nums)-1

while l<=h:
if nums[l]<=nums[h]:
return nums[l]
m=(l+h)//2

if nums[m]<nums[m-1] and nums[m]<nums[m+1]:
return nums[m]
if nums[l]<=nums[m]:
l=m+1
else:
h=m-1
return 444444