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
75 changes: 75 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#Search a 2D Matrix

#Space complexity O(1), Time complexity O(log(m) + log(n))
#Binary search on 1st column of rows will be used to identify which row target can be present
#Binary search on that row will tell if target is present or not

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

# # edge case checks
# if matrix[0][0] > target:
# return False

# if matrix[len(matrix)-1][len(matrix[0])-1] < target:
# return False

# #1st for the input using binary search found which row does the number belongs using 1st column of each row
# rowLow = 0
# highRow = len(matrix) - 1

# while (rowLow<highRow):
# mid = (rowLow+highRow)//2 + 1
# # if we found target return true
# if matrix[mid][0] == target:
# return True

# if target < matrix[mid][0] :
# # target is before the mid
# highRow = mid - 1
# else:
# rowLow = mid

# # once and high are equals means we found the row in which the target can lie

# # now binary search on that row

# lowC = 0
# highC = len(matrix[rowLow]) - 1

# while lowC <= highC:
# mid = (lowC + highC)//2
# if matrix[rowLow][mid] == target:
# return True
# if target < matrix[rowLow][mid] :
# highC = mid -1
# else:
# lowC = mid + 1

# return False

# This solution is more cleaner however, might be a bit slower due to the extra mathematical computations
# The time complexity for both will be O(log(m*n))

def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
# assuming it is a long array just place in 2d patrix
left = 0
right = len(matrix) * len(matrix[0]) - 1
rowLength = len(matrix[0])
while left<=right:
mid = (left+right)//2
#Now we need to find where in the 2d matrix this mid will be present
#if we take the length of 1st row, devision with that should give us the row number
#while modulus will give the column
row = mid // rowLength
column = mid % rowLength
element = matrix[row][column]
print("left is {}, right is {}, mid is {}, row is {}, column is {} and element at mid is {}".format(left, right, mid, row, column,element))
if element == target:
return True

if target >= matrix[row][column] :
left = mid + 1
else:
right = mid - 1
return False
25 changes: 25 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution:
def search(self, nums: List[int], target: int) -> int:
left = 0
right = len(nums) - 1

while left <= right:
mid = left+(right-left)//2
if nums[mid] == target:
return mid

if nums[mid] >= nums[left]:
# left is sorted, or both are sorted
if target >= nums[left] and target < nums[mid]:
right = mid - 1
else:
left = mid + 1

else:
# right is sorted
if target > nums[mid] and target <= nums[right]:
left = mid + 1
else:
right = mid -1

return -1
33 changes: 33 additions & 0 deletions Problem3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# """
# 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:
#search the 1st occurence of the array out of bound index to get the length
#of the arrya, then normal binary search in sorted array
left = 0
right = 1
valAtRight = reader.get(right)
while target >= valAtRight:
if valAtRight == target:
return right
left = right
right = right*2
valAtRight = reader.get(right)

while left <= right:
mid = left + (right-left)//2
val = reader.get(mid)
if val == target:
return mid

if target > val:
left = mid+1
else:
right = mid -1

return -1