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
35 changes: 35 additions & 0 deletions 01-Search-a-2D-Matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Problem 1: Search a 2D Matrix (https://leetcode.com/problems/search-a-2d-matrix/)

# Time Complexity: O(log(m*n)) where m is the number of rows and n is the number of columns
# Space Complexity: O(1)
# Did this code successfully run on Leetcode : Yes
# Three line explanation of solution in plain english
# We treat the 2D matrix as a 1D sorted array and perform binary search.
# We calculate the mid index and convert it back to 2D indices to access the matrix elements.


class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
if not matrix or len(matrix[0])==0:
return False

m = len(matrix)
n = len(matrix[0])

low = 0
high = m*n-1
while low <= high:
mid = low + (high - low)//2
r = mid//n
c = mid%n

if matrix[r][c] == target:
return True

elif target < matrix[r][c]:
high = mid -1

elif target > matrix[r][c]:
low = mid +1

return False
36 changes: 36 additions & 0 deletions 01-Search-in-Infinite-Sorted-Array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Problem 3: Search in Infinite Sorted Array (https://leetcode.com/problems/search-in-infinite-sorted-array/)

# Time Complexity: O(log n) where n is the number of elements in the array
# Space Complexity: O(1)
# Did this code successfully run on Leetcode : Yes
# Three line explanation of solution in plain english
# We first find the bounds where the target could be by exponentially increasing the high index.
# Once the bounds are found, we perform a standard binary search within those bounds.
# We use the ArrayReader interface to access elements, which returns 2^31 - 1 for out-of-bounds indices.


class Solution:
def search(self, reader: 'ArrayReader', target: int) -> int:

if not reader:
return None

low = 0
high = 1

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

while low <= high:
mid = low + (high - low)//2

if reader.get(mid) == target:
return mid

elif reader.get(mid) > target:
high = mid - 1
else:
low = mid + 1

return -1
37 changes: 37 additions & 0 deletions 02-Search-in-a-Rotated-Sorted-Array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Problem 2: Search in a Rotated Sorted Array (https://leetcode.com/problems/search-in-rotated-sorted-array/)

# Time Complexity: O(log n) where n is the number of elements in the array
# Space Complexity: O(1)
# Did this code successfully run on Leetcode : Yes
# Three line explanation of solution in plain english
# We perform binary search while determining which side (left or right) is sorted.
# If the target lies within the sorted side, we adjust our search to that side.
# Otherwise, we search in the other side.

class Solution:
def search(self, nums: List[int], target: int) -> int:
if not nums:
return None

low, high = 0, len(nums)-1

while low <= high:
mid = low + (high-low)//2

if nums[mid] == target:
return nums.index(target)

# left sorted
if nums[low] <= nums[mid]:
if target >= nums[low] and target < nums[mid]:
high = mid -1
else:
low = mid + 1
# right sorted
if nums[mid] <= nums[high]:
if target > nums[mid] and target <= nums[high]:
low = mid + 1
else:
high = mid - 1

return -1