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
43 changes: 43 additions & 0 deletions search_2d_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
Find the row of the target element first
and then using that row do binary search to find the target
Time Complexity : O(log mn )
Space Complexity : O(1)
"""
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:

top = 0
bottom = len(matrix) - 1


while top <= bottom:
mid = (top + bottom) // 2

if target > matrix[mid][-1]:
top = mid + 1
elif target < matrix[mid][0]:
bottom = mid - 1
else:
break


row = (top + bottom) // 2

l, r = 0, len(matrix[0]) - 1

while l <= r:

mid = (l + r) // 2

if target > matrix[row][mid]:
l = mid + 1
elif target < matrix[row][mid]:
r = mid - 1
else:
return True

return False



33 changes: 33 additions & 0 deletions search_in_rotated_sorted_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
Check the array is left sorted or right sorted array and then check if the target
is present in the left half or right

Time Complexity: O(log n)
Space Complexity: O(1)
"""
class Solution:
def search(self, nums: List[int], target: int) -> int:

l, r = 0, len(nums) - 1

while l <= r:

mid = (l + r) // 2

if nums[mid] == target:
return mid

# Left sorted
if nums[l] <= nums[mid]:

if nums[l] <= target and nums[mid] > target:
r = mid - 1
else:
l = mid + 1
else:
# Right sorted
if nums[mid] < target and nums[r] >= target:
l = mid + 1
else:
r = mid - 1
return -1
68 changes: 68 additions & 0 deletions search_in_sorted_array_unknow_size.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
This is an interactive problem.

You have a sorted array of unique elements and an unknown size. You do not have an access to the array but you can use the ArrayReader interface to access it. You can call ArrayReader.get(i) that:

returns the value at the ith index (0-indexed) of the secret array (i.e., secret[i]), or
returns 231 - 1 if the i is out of the boundary of the array.
You are also given an integer target.

Return the index k of the hidden array where secret[k] == target or return -1 otherwise.

You must write an algorithm with O(log n) runtime complexity.



Example 1:

Input: secret = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in secret and its index is 4.
Example 2:

Input: secret = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in secret so return -1.

Use the helper method and move the right pointer until the target is greater than the right pointer
move left pointer to right pointer value and move left pointer to prev left pointer multiple with 2

After getting the left and right pointer do normal binary search

Time Complexity: O(log n)
Space Complexity: O(1)
"""


# """
# 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:
l = 0
r = 1
while l <= r:

if reader.get(r) > target:
break

l = r
r = 2 * r

while l <= r:

mid = (l + r) // 2

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

if reader.get(mid) > target:
r = mid - 1
else:
l = mid + 1

return -1