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
31 changes: 31 additions & 0 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Time Complexity: O(log(m*n))
# Space Complexity: O(1)
# Were you able to run the code on Leetcode: Yes
# Any problem you faced while coding this: No

class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:

m = len(matrix)
n = len(matrix[0])

# set low and high to 0 and last flattened index
low,high = 0, m*n -1

while low<=high:
mid = low + (high-low)//2

# get the row and col idx by bounding it by n
# since array is sorted row wise
row = mid//n
col = mid%n

if matrix[row][col] == target:
return True
elif matrix[row][col] < target:
# search in right side of mid element
low = mid + 1
elif matrix[row][col] > target:
# search left side of mid element
high = mid - 1
return False
40 changes: 40 additions & 0 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Time Complexity: O(log n)
# Space Complexity: O(1)
# Were you able to run the code on Leetcode: Yes
# Any problem you faced while coding this: No

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

if not nums or len(nums) == 0:
return -1

low = 0
high = len(nums)-1

while(low<=high):
mid = low + (high-low) //2

if nums[mid] == target:
return mid

# check which side is sorted
if nums[low] <= nums[mid]:
# left side sorted
if nums[low] <= target < nums[mid]:
# if target is in the sorted side then search there by decrementing high to mid -1
high = mid - 1
else:
low = mid+1
else:
# right side sorted
if nums[mid] < target <= nums[high]:
# if target is in the sorted side then search there by incrementing low to mid+1
low = mid+1
else:
high = mid -1

return -1



50 changes: 50 additions & 0 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Time Complexity: O(log n)
# Space Complexity: O(1)
# Were you able to run the code on Leetcode: No
# Any problem you faced while coding this: No


# This is ArrayReader's API interface.
# You should not implement it, or speculate about its implementation
# """
# class ArrayReader:
# def get(self, index: int) -> int:


class Solution:
def search(self, reader, target):
"""
:type reader: ArrayReader
:type target: int
:rtype: int
"""
low, high = 0, 1

while reader.get(high) <= target:
# start with small nos, keep increasing high by factor of 2 for binary search until high is greater or equal to target
low = high
high *= 2

return self.searchRange(reader, target, low, high)




def searchRange(self, reader, target, low, high)-> int:

while low<=high:

mid = low+(high-low) //2

if reader.get(mid) == target:
return mid

# search in left window if mid value greater than target else right window
if reader.get(mid) > target:
high = mid - 1
else:
low = mid + 1

return -1