diff --git a/rotated_sorted_array.py b/rotated_sorted_array.py new file mode 100644 index 00000000..643ebab9 --- /dev/null +++ b/rotated_sorted_array.py @@ -0,0 +1,28 @@ +# Time Complexity O(logn) +class Solution: + def search(self, nums: List[int], target: int) -> int: + start = 0 + end = len(nums)-1 + while start <= end: + mid = start + (end-start)//2 + + if target == nums[mid]: + return mid + + # find if element is in which half. One half is guarenteed to be sorted. + if nums[start] <= nums[mid]: + # if left is sorted. Check if element can fall on the left side. + if target < nums[mid] and target >= nums[start]: + end = mid-1 + else: + # go to un-sorted side since element can't be found in sorted side. + start = mid+1 + else: + # if right is sorted check if element can fall in the right side. + if target > nums[mid] and target<= nums[end]: + start = mid+1 + else: + # go to un-sorted side since eleemnt can't be found in sorted side. + end = mid-1 + # if not found + return -1 \ No newline at end of file diff --git a/search_in_unknown_array.py b/search_in_unknown_array.py new file mode 100644 index 00000000..9dba863a --- /dev/null +++ b/search_in_unknown_array.py @@ -0,0 +1,26 @@ +# """ +# This is ArrayReader's API interface. +# You should not implement it, or speculate about its implementation +# """ +#class ArrayReader: +# def get(self, index: int) -> int: + +# Time complexity - O(logn) +# Straight forware binary search solution on 10 power 4 sized array. +class Solution: + def search(self, reader: 'ArrayReader', target: int) -> int: + start = 0 + end = 10 ** 4 + while start <= end: + mid = start + (end - start)//2 + arr_val = reader.get(mid) + # end condition 1 + if arr_val == target: + return mid + # decision 1 & out of bounds + if arr_val > target: + end = mid -1 + else: + # if target is lower than mid + start = mid +1 + return -1 \ No newline at end of file diff --git a/search_matrix.py b/search_matrix.py new file mode 100644 index 00000000..4aa29a74 --- /dev/null +++ b/search_matrix.py @@ -0,0 +1,31 @@ +# Time taken to solve the problem = 20 mins +# Time complexity - O(logmn) +# The 2-D array gets filled row wise. If flattended completely. It's a sorted list. Binary search on sorted list is straight forward. +# To get the co-ordinates of the mid element in the 2d array we need to use columns since the array is filled row wise. Dividing by cols gives x cords and mod by cols gives y cords. +# Since X is the nth part and y is the offest in the nth part. +class Solution: + def get_cordinates(self, mid, rows, cols): + co_ordinates_x = mid//cols + co_oridnates_y = mid % cols + return co_ordinates_x, co_oridnates_y + + def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: + rows = len(matrix) + cols = len(matrix[0]) + start = 0 + end = (rows*cols) -1 + while start <= end: + mid = start + (end-start)//2 + x, y = self.get_cordinates(mid, rows, cols) + print(matrix[x][y]) + if matrix[x][y] == target: + return True + if matrix[x][y] < target: + start = mid+1 + else: + end = mid - 1 + return False + + + + \ No newline at end of file