From c1473bba695777d19d319c97254457879dac86c7 Mon Sep 17 00:00:00 2001 From: RITIKA CHAUBE Date: Fri, 5 Jun 2026 20:02:48 -0400 Subject: [PATCH] Done with binary search 1 --- problem-1.py | 29 +++++++++++++++++++++++++++++ problem-2.py | 29 +++++++++++++++++++++++++++++ problem-3.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 problem-1.py create mode 100644 problem-2.py create mode 100644 problem-3.py diff --git a/problem-1.py b/problem-1.py new file mode 100644 index 00000000..5ab9f973 --- /dev/null +++ b/problem-1.py @@ -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=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 \ No newline at end of file diff --git a/problem-3.py b/problem-3.py new file mode 100644 index 00000000..16da8848 --- /dev/null +++ b/problem-3.py @@ -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: + high=mid-1 + else: + low=mid+1 + + return -1 + +secret=[-1,0,3,5,9,12] +target=9 \ No newline at end of file