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
29 changes: 29 additions & 0 deletions problem-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# SEARCH A 2D MATRIX
# TIME COMPLEXITY: O(log N*M) where N denotes the rows and M denotes the columns
# SPACE COMPLEXITY: O(1)
# Any problem you faced while coding this : Had some hiccup while writing the calc_row and
# calc_col code as I knew the concept but hadsome error in implementation
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
low=0
row=len(matrix)
col=len(matrix[0])
high=row*col-1
while low<high:
mid=(high+low)//2
calc_row=mid//col
calc_col=mid%col
if matrix[calc_row][calc_col]==target:
return True
elif matrix[calc_row][calc_col]<target:
low=mid+1
else:
high=mid
final_row=low//col
final_col=low%col
return matrix[final_row][final_col]==target
29 changes: 29 additions & 0 deletions problem-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# SEARCH IN A ROTATED SORTED ARRAY
# TIME COMPLEXITY: O(log N) where N denotes the number of elements
# SPACE COMPLEXITY: O(1)
# Any problem you faced while coding this : ran into some edge cases error while doing the
# submission but then was able to understand the flaw and implement the code
class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
start=0
end=len(nums)-1
while start<=end:
mid=(start+end)//2
if nums[mid]==target:
return mid
elif nums[mid]>=nums[start]:
if nums[start]<=target<=nums[mid]:
end=mid-1
else:
start=mid+1
else:
if nums[mid]<=target<=nums[end]:
start=mid+1
else:
end=mid-1
return -1
31 changes: 31 additions & 0 deletions problem-3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# SEARCH IN INFINITE SORTED ARRAY
# TIME COMPLEXITY: O(log N) where N is the number of elements we get when
# high > target occure
# SPACE COMPLEXITY: O(1)
# Any problem you faced while coding this : Had a clear idea on the concept its just that
# given its a LC Premium question was not able to give it a dry run referred the video
# and S30 page where the question and constrains were written


class Solution:
def search(self, reader:'ArrayReader', target:int)->int:
low=0
high=1

while reader.get(high)<target:
low=high
high=high*3

while low<=high:
mid=(low+high)//2
if reader.get(min)==target:
return mid
elif reader.get(mid)>target:
high=mid-1
else:
low=mid+1

return -1

secret=[-1,0,3,5,9,12]
target=9