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
34 changes: 34 additions & 0 deletions Problem1_Search2DMatrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Time Complexity : O(log(m*n)) where m is rows and n is columns
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

# Approach: Treat the 2D matrix as a 1D sorted array and perform binary search.
# Calculate row and column indices from the 1D index using division and modulo operations.
# Compare target with middle element and adjust search boundaries accordingly.

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

m = len(matrix)
n = len(matrix[0])
left = 0
right = m * n - 1

while left <= right:
mid = left + (right - left) // 2
row = mid // n
col = mid % n
mid_value = matrix[row][col]

if mid_value == target:
return True
elif mid_value < target:
left = mid + 1
else:
right = mid - 1

return False

40 changes: 40 additions & 0 deletions Problem2_SearchRotatedSortedArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Time Complexity : O(log n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

# Approach: Use binary search with additional logic to handle rotation.
# First identify which half is sorted by comparing mid with left boundary.
# Then check if target lies in the sorted half, otherwise search in the other half.

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

left = 0
right = len(nums) - 1

while left <= right:
mid = left + (right - left) // 2

if nums[mid] == target:
return mid

# Check if left half is sorted
if nums[left] <= nums[mid]:
# Target is in the left sorted half
if nums[left] <= target < nums[mid]:
right = mid - 1
else:
left = mid + 1
else:
# Right half is sorted
# Target is in the right sorted half
if nums[mid] < target <= nums[right]:
left = mid + 1
else:
right = mid - 1

return -1

47 changes: 47 additions & 0 deletions Problem3_SearchInfiniteSortedArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Time Complexity : O(log n) where n is the position of target
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

# Approach: First find the search boundaries by exponentially expanding the range.
# Once boundaries are found, perform standard binary search within those bounds.
# Handle IndexError when accessing elements beyond array length.

# """
# 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 Solution:
def search(self, reader: 'ArrayReader', target: int) -> int:
# First, find the boundaries
left = 0
right = 1

# Expand right boundary until we find a range that contains target
# or until we go out of bounds
while reader.get(right) < target:
left = right
right = right * 2

# Now perform binary search within the found boundaries
while left <= right:
mid = left + (right - left) // 2
try:
mid_value = reader.get(mid)
except IndexError:
# If we go out of bounds, adjust right boundary
right = mid - 1
continue

if mid_value == target:
return mid
elif mid_value < target:
left = mid + 1
else:
right = mid - 1

return -1