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
23 changes: 23 additions & 0 deletions PeakElement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
''' Time Complexity : O(log n)
Space Complexity : O(1)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No

Your code here along with comments explaining your approach

Approach : Comparing if mid greater than its both neighbors, return mid
Else , slide towards greater neighbor
'''
class Solution:
def findPeakElement(self, nums: List[int]) -> int:
n = len(nums)
l, h = 0, n-1
while l <= h:
mid = (l+h) // 2
print(l,h,mid)
if ((mid == 0 or nums[mid] > nums[mid-1]) and (mid == n-1 or nums[mid] > nums[mid+1])):
return mid
elif nums[mid+1] > nums[mid]:
l = mid + 1
else:
h = mid - 1
55 changes: 55 additions & 0 deletions findFirstLastSortedArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
''' Time Complexity : O(log n)
Space Complexity : O(1)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No

Your code here along with comments explaining your approach

Approach : Implementing 2 binray search - for First and Last index
1. Find First index by comparing with [mid-1],
2. Use first index as low for Second binary search
3. Find Second index by comparing with [mid+1]

'''

class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:

def firstBinarySearch(nums,l,h):
while l <= h:
mid = (l+h)//2
print(l, h , mid)
if nums[mid] == target:
if mid == 0 or nums[mid-1] != target:
return mid
else:
h = mid - 1

elif nums[mid] < target:
l = mid + 1
else:
h = mid - 1
return -1

def lastBinarySearch(nums,l,h):
while l <= h:
mid = (l+h)//2
if nums[mid] == target:
if mid == n-1 or nums[mid+1] != target:
return mid
else:
l = mid + 1

elif nums[mid] < target:
l = mid + 1
else:
h = mid - 1
return -1

n = len(nums)
first = firstBinarySearch(nums,0,n-1)
if first == -1:
return [-1,-1]
last = lastBinarySearch(nums,first,n-1)
return [first,last]

43 changes: 43 additions & 0 deletions findMinSortedArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
''' Time Complexity : O(log n)
Space Complexity : O(1)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No

Your code here along with comments explaining your approach

'''
# --- Solution 1 : ------
''' Approach : Comparing if mid less than its neighbours, return mid
Else , find the sorted array and move to opposite direction
'''
class Solution:
def findMin(self, nums: List[int]) -> int:
n = len(nums)
l, h = 0, n-1
while l <= h:
if nums[l] <= nums[h]:
return nums[l]
mid = (l+h) // 2
if nums[mid] < nums[mid-1] and nums[mid] < nums[mid+1]:
return nums[mid]
elif nums[l] <= nums[mid]:
l = mid + 1
else:
h = mid -1

# --- Solution 2 : ------
''' Approach : If mid is greater than high, means left side is sorted
and min element lies in right side.
'''
class Solution:
def findMin(self, nums: List[int]) -> int:
n = len(nums)
l, h = 0, n-1
while l < h:
mid = (l + h) // 2
if nums[mid] > nums[h]:
# left side is sorted and min lies in right side
l = mid + 1
else:
h = mid
return nums[l]