Skip to content

Dharma Binary-search-1 submission #2318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
98 changes: 98 additions & 0 deletions search_a_2d_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
'''
Appraoach:

TIme Complexity: O(logm * logn) where m is the number of rows and n is the number of columns.
Space Complexity: O(1)
'''

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



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

rows_low=0
rows_high= m-1


# Finding the row using binary search.
while(rows_low<=rows_high):

# Find the mid of the row
rows_mid = (rows_low+rows_high)//2


if(matrix[rows_mid][0]<=target and matrix[rows_mid][n-1]>=target):

col_low = 0
col_high = n-1

while(col_low<=col_high):
col_mid = (col_low+col_high)//2


if(matrix[rows_mid][col_mid]==target):
return True

elif(matrix[rows_mid][col_mid]>target):
col_high = col_mid-1

else:
col_low = col_mid+1
return False

elif(matrix[rows_mid][0]<target):
rows_low=rows_mid+1
else:
rows_high=rows_mid-1


return False



'''
Approach 2:
'''

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

'''
As problem explicitly mentions to solve the problem in O(log(m*n))
I am applying the same methodology here as discussed in the class.

Assuming there is a flattened array, and apply the binary search.

In Assumption
rows = mid/cols
cols = mid%cols

Time Complexity: O(log(m*n)) where m is the number of rows and n is the number of columns.
Space Complexity: O(1)


'''

m= len(matrix)
n= len(matrix[0])
low= 0
high = m*n-1

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

r = mid//n
c = mid%n

if matrix[r][c]==target:
return True
elif(matrix[r][c]>target):
high = mid-1
else:
low = mid+1
return False


50 changes: 50 additions & 0 deletions search_in_rotated_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class Solution:
def search(self, nums: List[int], target: int) -> int:

'''
The intricacy of solving this problem is you have to understand, which part of the mid is sorted
Is it left or right?
Once you find that, then you figure out rest
Like whether the number which ever you are searching in the sorted or not.

I have to practice this problem atleast 3 more times to get the hang of it.

Time Complexity: O(log n)
Space Complexity: O(1)
'''


low =0
high= len(nums)-1

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

print(mid)

if(nums[mid]==target):
return mid

# This is the pivotal moment where the decision happen.
elif(nums[mid]>=nums[low]):
# which mean left part of the array is sorted:
# Now lets check if target is present in this part


if(nums[low]<=target and nums[mid]>target):
high=mid-1
else:
low = mid+1

else:

if(nums[mid]<target and target<=nums[high]):
low=mid+1
else:
high=mid-1




return -1

48 changes: 48 additions & 0 deletions search_unknown_size.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# """
# This is ArrayReader's API interface.
# You should not implement it, or speculate about its implementation
# """
#class ArrayReader:
# def get(self, index: int) -> int:


'''
The problem currently says

O(logn)+O(logk)

The reason we are not directly using binary search over this problem to make a
balance between logn and logk, instead of directly applying it all the pressure on the logK

Time Complexity: O(log n)
Space Complexity: O(1)
'''

class Solution:
def search(self, reader: 'ArrayReader', target: int) -> int:


low = 0
high=1

while(target>reader.get(high)):
low = high
high = high*2


while(low<=high):

mid = (low+high)//2

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

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

return -1