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 Search2DMatrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from typing import List


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

if not matrix or not matrix[0]:
return False

row = len(matrix)
col = len(matrix[0])

#now we will flatten 2D matrix to imaginary 1D Array.
#but we will map the values in 1D array to corresponding pos in 2D matrix
#Treat the 2D matrix as a 1D sorted array using index mapping (mid / n, mid % n).
low = 0
total = row*col
high = total-1

while low <= high:
mid = low + (high - low)//2
#Mapping 1D index → 2D index
if matrix[mid//col][mid%col] == target:
return True
elif target < matrix[mid//col][mid%col]:
high = mid-1
else:
low = mid+1

return False

# Time Complexity = O(log(m*n)) = O(logm+logn) #since we are doing BS on the flattened 1D array which has m*n elements
# Space Complexity: O(1)

# Integer division tells us how many full rows we skip
# Every col elements → move to next row
# Modulo tells us position inside the row or how many cols we skip inside a row.
31 changes: 31 additions & 0 deletions rotatedSortedArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution:
def search(self, nums: List[int], target: int) -> int:
low = 0
high = len(nums)-1

while low <high:
mid = low + (high - low)//2 #to take care of integer overflow

if nums[mid] == target:
return mid
#check left or right half is sorted
if nums[low] <= nums[mid]:
#now check if the target is in this range
if nums[low] <= target and nums[mid]>target:
high = mid
else:
low = mid+1

else:
#means right side is sorted in asc
if nums[mid] < target and nums[high] >= target:
low = mid+1
else:
high = mid
if nums[low] == target:
return low

return -1

# Time Complexity: O(logN)
# Space Complexity: O(1)
34 changes: 34 additions & 0 deletions searchUnkownSizedArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# """
# 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: 'ArrayReader', target: int) -> int:

#first take double steps to find the range where the target falls
low = 0
high = 1

#increasing search space by 2 is also a Binary Search
while target > reader.get(high): #takes some m steps, stop when target is within the range
low = high
high = high*2

#once we get range do simple BS
while low <= high:
mid = low + (high - low)//2

if reader.get(mid) == target:
return mid
elif target < reader.get(mid):
high = mid-1
else:
low = mid+1
return -1

# Time Complexity is O(logm)+O(logk) k is the no of elements between low and high we do BS inside, logm is for the expansion of the range.
# Space Complexity is O(1)
# We can't take hugh strides like high*3 or high*100 because then m inc drastically and k reduces drstically. To keep a balance we take *2 which is optimal time complexity. We don't slow down once computation over the other.