Skip to content

Done Binary-Search-1 #2334

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

Open
wants to merge 1 commit 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
42 changes: 42 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Time Complexity : O(log m*n)
# Space Complexity : O(1)
#Did this code successfully run on Leetcode : Yes
#Any problem you faced while coding this : No
# Three line explanation of solution in plain english:
#1. We use binary search to find the target in a 2D matrix for this we flatten the 2d matrix to an array.
#2. We treat the 2D matrix as a 1D array by calculating the row and column indices from the mid index.
#we use r and c by dividing mid by n (number of columns) and taking the modulus with n respectively.
#3. If the target is found, we return True; otherwise, we adjust the search range based on the comparison with the mid element.

class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
#get the number of rows and columns in the matrix
m=len(matrix)
n=len(matrix[0])
#low will be the starting index and high will be the last index of the flattened matrix
low=0
high=m*n-1
#binary search to find the target in the flattened matrix
while low<=high:
mid=low+(high-low)/2
#calculate the row and column indices from the mid index
#if you divide mid by number of columns n, you get the row index r
#and if you take mid modulo n, you get the column index c
r=mid/n
c=mid%n
#if the target is found at the mid position, return True
if matrix[r][c]==target:
return True
#if the target is less than the mid element, adjust the high pointer to mid-1 and search in the left half
# if the target is greater than the mid element, adjust the low pointer to mid+1 and search in the right half
if matrix[r][c]<target:
low=mid+1
else:
high=mid-1
#if the target is not found, return False
return False
43 changes: 43 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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
# Three line explanation of solution in plain english:
#1. We use binary search to find the target in a rotated sorted array.
#2. We check if the left side of the array is sorted and if the target lies
#3. If the right side is sorted, we check if the target lies in that range and adjust the pointers of high and low accordingly.




class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
low=0
high=len(nums)-1
while low<=high:
mid=low+(high-low)/2
#if the target is at mid, return mid position
if target==nums[mid]:
return mid
#left side sorted
elif nums[low]<=nums[mid]:
#check if target lies in the left side sorted array
#if it does, reduce the high pointer to mid-1, else increase the low pointer to mid+1
if nums[low]<=target and nums[mid]>target:
high=mid-1
else:
low=mid+1
#right side sorted
else:
#if it does, increase the low pointer to mid+1, else reduce the high pointer to mid
if nums[mid]<target and nums[high]>=target:
low=mid+1
else:
high=mid-1
#if the target is not found, return -1
return -1
39 changes: 39 additions & 0 deletions Problem3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Time Complexity : O(log n)
# Space Complexity : O(1)
#Did this code successfully run on Leetcode : No
#Any problem you faced while coding this : No
# Three line explanation of solution in plain english:
# 1. The solution uses an exponential search to find the range where the target might be located.
# 2. It then applies binary search within that range to find the exact index of the target.
# 3. The reader.get method is used to access elements, simulating a scenario where the array size is unknown.


class Solution:
def search(self, reader, target):
low = 0
high = 1

# Expand the search range exponentially
# until the value at high index is greater than or equal to the target
while reader.get(high) < target:
low = high
high *= 2

return self.binarySearch(reader, target, low, high)
#
# Perform binary search within the determined range
def binarySearch(self, reader, target, low, high):
while low <= high:
mid = low + (high - low) // 2
# Use reader.get to access the value at mid
val = reader.get(mid)

# Check if the value at mid is equal to the target
if val == target:
return mid
elif val > target:
high = mid - 1
else:
low = mid + 1
# If the target is not found, return -1
return -1