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
26 changes: 26 additions & 0 deletions Find_Minimum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Time Complexity : O(logn)
# 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
# Do binary serach and check if mid is greater than right , then minimum is in right elese left.
# One side is always sorted.
import sys
class Solution(object):
def findMin(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
l, r = 0, len(nums) - 1
while l < r:
mid = (l + r) // 2
#right half
if nums[mid] > nums[r]:
l = mid + 1
#left half
else:
r = mid
return nums[r]

29 changes: 29 additions & 0 deletions Find_peak.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Time Complexity : O(log n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : missed first and last edge case.

# Your code here along with comments explaining your approach
# 2 stacks - 1 for storing actual pushed values,
# 2 - for storing minimum values at each level,
# These 2 stacks must have 1:1 mapping

class Solution(object):
def findPeakElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
l = 0
n = len(nums)
r = n - 1

while l <= r:
mid = l + ( r - l ) // 2
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] and mid < n-1:
l = mid + 1
else:
r = mid - 1
return - 1
51 changes: 51 additions & 0 deletions First_Last.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Time Complexity : O(logn) -> 2 binary search = O(2logn) = O(logn)
# 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
# Find first occurence of target using firstposition function- returns the first index
# Check if firstposition function returns -1 then no target exists.
# Else find the last position of the target using other lastposition function similar to first.
class Solution(object):
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
def firstposition(nums,target,l,r):
while l <= r:
mid = l + (r - l)// 2
if nums[mid] == target:
if mid == 0 or nums[mid -1] != target:
return mid
else:
r = mid - 1
elif nums[mid] < target:
l = mid + 1
else:
r = mid - 1
return -1

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

n = len(nums) -1
first = firstposition(nums,target,0,n)
if first == -1:
return [-1,-1]
last = lastposition(nums,target,0 ,n)
return [first,last]