Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary Search-1 Completed #2043

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
28 changes: 28 additions & 0 deletions binarySearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# // Time Complexity : O(log(n)) since binary search
# // Space Complexity : O(1) No aux variables
# // Did this code successfully run on Leetcode : Yes
# // Any problem you faced while coding this : Removed additinal case for both left sorted and right sorted arrays when one if condition is enough.

# we will implement a search operation in a rotates sorted array.
# We first find middle and then using pointer on each sides find if it is right sorted or left sorted
# at some point all pointers will land on the same value target or target range.


class Solution:
def search(self, nums: list[int], target: int) -> int:
low = 0
high = len(nums) - 1
while low <= high:
mid = (low + high) // 2
if nums[mid] == target:
return mid #target is in the middle
elif nums[mid] < target: #target is on the right (other condition is unncessary)
low = mid + 1 #shift low
else:
high = mid - 1 #shift high

arr = [-1,0,2, 3, 4, 10, 40, 123]
x = 40

result = Solution().search(arr, x)
print(result)
32 changes: 32 additions & 0 deletions infSortedArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# // 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

# We know that array is sorted.
# Initialize : l=0 and h = 1, while target not in range (l,h); l = h , h = h*2
# Then we will use binary search for newly found range
# """
# 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:
low = 0
high = 1
while reader.get(high) < target:
low = high
high = high*2

while low <= high: # as soon as range is found, we know l and h
mid = low + (high-low)//2 # we need a pivot point to find the target
if reader.get(mid)== target: # binary search
return mid
elif reader.get(mid)< target:
low = mid + 1
else:
high = mid -1
return -1
49 changes: 49 additions & 0 deletions search2DMatrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# // Time Complexity : O(log(n)) = O(log(m)+log(n) because binary search
# // Space Complexity :O(1) because no Auxilliary variables taking space
# // Did this code successfully run on Leetcode : Yes
# // Any problem you faced while coding this : Calculating mid required "//" instead of '/'. Accidently used 'm' instead of "mid"

# // Your code here along with comments explaining your approach in three sentences only
# Imagine the 2D array as a simple array. Then find a middle element and compare it with the target.
# Do binary search using that middle element and target.

class Solution:
def searchMatrix(self, matrix: list[list[int]], target: int) -> bool:
m = len(matrix) # m = rows
n = len(matrix[0]) # n = columns
low, high = 0, m*n-1 # 2 pointers

while low <= high:
mid = low +(high-low)//2 # find mid. Used "//"
r = mid // n
c = mid % n
if matrix[r][c] == target: # target found RETURN True
return True
elif matrix[r][c] > target: # target lies to the left of mid
high = mid - 1
else: # target lies to right of mid
low = mid + 1

return False # target not found RETURN False

x = Solution.searchMatrix(self = Solution(), matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 4)
print(x)






# arr = [[1,2,3],[4,5,6]]
# x = 5
# result = Solution().searchMatrix(arr, x)
# print(result)


# Bruteforce linear search thru matrix O(n^2) Works
# class Solution:
# def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
# for i in matrix:
# if target in i:
# return True
# return False