From 24ea3bd95b575c863b39830922bb666fc4158fff Mon Sep 17 00:00:00 2001 From: keerthianand03 <60270758+keerthianand03@users.noreply.github.com> Date: Sat, 21 Jun 2025 12:26:39 -0500 Subject: [PATCH] Add files via upload --- 2DMatrix.py | 39 +++++++++++++++++++++++++++++++++++++++ InfiniteArray.py | 32 ++++++++++++++++++++++++++++++++ RotatedSortedArray.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 2DMatrix.py create mode 100644 InfiniteArray.py create mode 100644 RotatedSortedArray.py diff --git a/2DMatrix.py b/2DMatrix.py new file mode 100644 index 00000000..fe3f6d40 --- /dev/null +++ b/2DMatrix.py @@ -0,0 +1,39 @@ +# Time Complexity : O(log(mn)) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes + +class Solution(object): + def searchMatrix(self, matrix, target): + if len(matrix) == 0: + return False + m = len(matrix) + n = len(matrix[0]) + low = 0 + high = (m*n) - 1 + while low <= high: + mid = low + (high - low)//2 + col = mid % n + row = mid // n + if matrix[row][col] == target: + return True + elif target < matrix[row][col]: + high = mid -1 + else: + low = mid + 1 + return False + + """ + :type matrix: List[List[int]] + :type target: int + :rtype: bool + """ + + + +obj = Solution() +output1 = obj.searchMatrix([[1,3,5,7],[10,11,16,20],[23,30,34,60]], 3) +print(output1) +output2 = obj.searchMatrix([[1,3,5,7],[10,11,16,20],[23,30,34,60]], 13) +print(output2) +output3 = obj.searchMatrix([[1]],0) +print(output3) \ No newline at end of file diff --git a/InfiniteArray.py b/InfiniteArray.py new file mode 100644 index 00000000..717d83fa --- /dev/null +++ b/InfiniteArray.py @@ -0,0 +1,32 @@ +# Time Complexity : O(log(n)) +# Space Complexity : O(1) + +class Solution(object): + def search(self, nums, target): + if len(nums) == 0: + return -1 + low = 0 + high = 1 + #Exponential search to finding bounds. + while True: + try: + if nums[high] < target: + low = high + high = high * 2 + else: + break + except IndexError: + break + #bineary search + while low <= high: + try: + mid = low + (high - low) //2 + if nums[mid] == target: + return mid + elif target < nums[mid]: + high = mid - 1 + else: + low = mid + 1 + except IndexError: + high = mid - 1 + return -1 \ No newline at end of file diff --git a/RotatedSortedArray.py b/RotatedSortedArray.py new file mode 100644 index 00000000..27c9da38 --- /dev/null +++ b/RotatedSortedArray.py @@ -0,0 +1,33 @@ +# Time Complexity : O(log(n)) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes + +class Solution(object): + def search(self, nums, target): + if len(nums) == 0: + return -1 + low = 0 + high = len(nums) - 1 + while low <= high: + mid = low + (high - low) //2 + if nums[mid] == target: + return mid + elif nums[low] <= nums[mid]: + if (target >= nums[low] and target < nums[mid]): + high = mid - 1 + else: + low = mid + 1 + else: + if (target <= nums[high] and target > nums[mid]): + low = mid + 1 + else: + high = mid - 1 + return -1 + +obj = Solution() +output1 = obj.search([4,5,6,7,0,1,2], 0) +print(output1) +output2 = obj.search([4,5,6,7,0,1,2], 3) +print(output2) +output3 = obj.search([1],0) +print(output3) \ No newline at end of file