diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..3c43208b --- /dev/null +++ b/Problem1.py @@ -0,0 +1,28 @@ +## Problem1 +# Search a 2D Matrix (https://leetcode.com/problems/search-a-2d-matrix/) +class Solution: + def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: + rows = len(matrix) + cols = len(matrix[0]) + + low = 0 + high = (rows * cols) - 1 + + # treat the matrix as 1D array + while low <= high: + mid = low + (high - low) // 2 + # convert 1D mid to row and column index of the matrix + # they are functions of the number of columns + row = mid // cols + col = mid % cols + + valueAtMid = matrix[row][col] + if valueAtMid == target: + return True + + if target < valueAtMid: + high = mid - 1 + else: + low = mid + 1 + + return False \ No newline at end of file diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..93d77b7c --- /dev/null +++ b/Problem2.py @@ -0,0 +1,31 @@ +## Problem2 +# Search in a Rotated Sorted Array (https://leetcode.com/problems/search-in-rotated-sorted-array/description/) + +class Solution: + def search(self, nums: List[int], target: int) -> int: + low = 0 + high = len(nums) - 1 + + while low < high: + mid = low + (high - low) // 2 + if nums[mid] == target: + return mid + + # check which part is sorted + # left sorted + if nums[low] <= nums[mid]: + # if target is within the left sorted part + # bring high to mid - 1 and run binary search on it + if nums[low] <= target and target < nums[mid]: + high = mid - 1 + else: # else seaerch on right + low = mid + 1 + # right sorted + else: + # if target is within the right sorted part + # bring low to mid + 1 and run binary search on it + if nums[mid] < target and target <= nums[high]: + low = mid + 1 + else: # else search on left + high = mid - 1 + return low if nums[low] == target else -1 \ No newline at end of file diff --git a/Problem3.py b/Problem3.py new file mode 100644 index 00000000..77b0216d --- /dev/null +++ b/Problem3.py @@ -0,0 +1,58 @@ +## Problem3 +# Search in Infinite sorted array (https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size/description/) +# """ +# 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 ArrayReader: + def __init__(self, arr: list[int]): + self.arr = arr + + def get(self, index: int) -> int: + if index < len(self.arr): + return self.arr[index] + return 2**31 - 1 + +class Solution: + def search(self, reader: "ArrayReader", target: int) -> int: + # Phase 1: find the low and high range + low = 0 + high = 1 + + # we will keep moving the right pointer by 2x until we reach an index > target index + while reader.get(high) < target: + low = high + high = high * 2 + + # Phase 2: Binary search + while low <= high: + mid = low + (high - low) // 2 + midVal = reader.get(mid) + if midVal == target: + return mid + + if midVal > target: + high = mid - 1 + else: + low = mid + 1 + + return -1 + + +# --- Execution Example --- +if __name__ == "__main__": + # Define your sorted array here + my_array = [-1, 0, 3, 5, 9, 12, 15, 18, 22, 45, 67, 89] + target_value = 9 + + # Instantiate the mock reader with your array + reader = ArrayReader(my_array) + + # Run the solution + sol = Solution() + result = sol.search(reader, target_value) + + print(f"Target {target_value} found at index: {result}") \ No newline at end of file