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
37 changes: 37 additions & 0 deletions Rotated_Sorted_Array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Time Complexity : O(log n) - binary search.
# Space Complexity : O(1) - storing only pointers.
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

#Your code here along with comments explaining your approach in three sentences only
#Search using binary search check if middle is the target if yes return.
#If not check if left is sorted or right is sorted. If left is sorted check if target is in the range of left else check in right.
class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""

l , r = 0 , len(nums) - 1
while l <= r:
mid = l + (r - l) // 2
if target == nums[mid]:
return mid

#left sorted
if nums[l] <= nums[mid]:
#right side
if target > nums[mid] or target < nums[l]:
l = mid + 1
else:
r = mid - 1
# Right sorted
else:
#left side
if target < nums[mid] or target > nums[r]:
r = mid - 1
else:
l = mid + 1
return -1
33 changes: 33 additions & 0 deletions Search_2D_Matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Time Complexity : O(log(m*n)) - binary search on rows and columns.
# Space Complexity : O(1) - storing only variables.
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : Trouble calculating rows and columns part.

#Your code here along with comments explaining your approach in three sentences only
#Converting 2d matric to imagneary 1d array.
#calculating r,c using mid to apply binary search and get return value.

class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""

m = len(matrix)
n = len(matrix[0])
l = 0
h = m * n - 1

while l <= h:
mid = l + (h - l) // 2
r = mid // n
c = mid % n
if matrix[r][c] == target:
return True
elif matrix[r][c] > target:
h = mid - 1
else:
l = mid + 1
return False
50 changes: 50 additions & 0 deletions Sorted_Array_Unknow_Size.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Time Complexity : O(log n)
# Space Complexity : O(1) - storing only variables.
# Did this code successfully run on Leetcode : No - premium subscription.
# Any problem you faced while coding this : No

#Your code here along with comments explaining your approach in three sentences only
#Until high is less than target or get out of bounds keep incresing it 2 times of low to get a range of numbers.
#Once range is found apply binary search to find the target.

class ArrayReader(object):
def __init__(self, arr):
self.data = arr

def get(self, index):
if index < len(self.data):
return self.data[index]
else:
# LeetCode returns 2^31 - 1 for out-of-bounds access
return 2147483647

class Solution(object):
def search(self, reader, target):
"""
:type reader: ArrayReader
:type target: int
:rtype: int
"""
low, high = 0, 1
while reader.get(high) < target:
low = high
high = high * 2
while low <= high:
mid = low + (high-low) // 2
if reader.get(mid) == target:
return mid
elif reader.get(mid) < target:
low = mid + 1
else:
high = mid - 1
return -1

# --- Local Testing Block ---
if __name__ == "__main__":
# Example usage:
test_data = [-1, 0, 3, 5, 9, 12]
target_val = 9

reader = ArrayReader(test_data)
sol = Solution()
print(f"Result: {sol.search(reader, target_val)}")