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

Solutions to Binary-Search-1 #2115

Open
wants to merge 4 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
7 changes: 0 additions & 7 deletions Sample

This file was deleted.

33 changes: 33 additions & 0 deletions rotatedsortedarray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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 majorly problem faced. Difficult in figuring out the conditions


// Your code here along with comments explaining your approach in three sentences only

class Solution:
def search(self, nums: List[int], target: int) -> int:
left = 0 # assigning the left pointer in the array
right = len(nums) - 1 # assigning the left pointer in the array

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

if nums[mid] == target: # if the target is equal to the mid, it returns the index of it
return mid

if nums[left] <= nums[mid]: # finding out if the left side of the mid is sorted
if nums[left] <= target < nums[mid]: # then finding if the target lies between the left and the mid pointers
right = mid - 1

else:
left = mid + 1

else: # finding out if the right side of the mid is sorted
if nums[mid] < target <= nums[right]:
left = mid + 1

else:
right = mid - 1
return -1 # returning -1 if the target is not found in the array
32 changes: 32 additions & 0 deletions search2Darray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Time Complexity : O(log(m*n))
# Space Complexity : O(log(m*n))
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : difficulty in assigning low and high pointers in the 2D array like took some time to figure that out


# Your code here along with comments explaining your approach in three sentences only

class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
rows = len(matrix) # calculating the number of rows
cols = len(matrix[0]) # calculating the number of columns

low = 0
high = rows * cols - 1

# performing binary search
while low <= high:
mid = (low + high) // 2
r = mid//cols # calculating rows of the 2D matrix
c = mid%cols # calculating the columns of the 2D matrix

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

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

else:
high = mid - 1

return False
35 changes: 35 additions & 0 deletions sortedarrayofinfinitesize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Time Complexity : O(log k) + O(log n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Since this is a premium problem, I'm unable to check the execution of the code
// Any problem you faced while coding this : no major difficulties, took little time in figuring out the conditions


// Your code here along with comments explaining your approach in three sentences only

class ArrayReader:
def get(self, index: int) -> int:
pass # Placeholder for the get method of ArrayReader.


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

while reader.get(high) < target and reader.get(high) != 2**31 - 1: # checking if the target element is greater than the high pointer and high does not exceed the maximum integer possible
low = high # moving the low to the high pointer position
high = 2 * high # incrementing the high pointer 2x times

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

if reader.get(mid) == target: # if mid equals target, it returns the mid index
return mid

if reader.get(mid) > target or reader.get(mid) == 2**31 - 1: # checking if the mid is greater than the target value
high = mid - 1 # if true, then the high will go to (mid-1)th position

else:
low = mid + 1 # else low will go to the next position after the mid

return -1 # if no target value found, then it will return -1