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
29 changes: 29 additions & 0 deletions problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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

class Solution(object):
def search(self, nums, target):
low = 0
high = len(nums) - 1

while low <= high:
mid = low + (high - low) // 2
#left sorted
if nums[mid] == target:
return mid
#left sorted
if nums[low] <= nums[mid]:
if nums[low] <= target < nums[mid]:
high = mid - 1
else:
low = mid + 1
else: #right sorted
if nums[mid] < target <= nums[high]:
low = mid + 1
else:
high = mid - 1
return -1


30 changes: 30 additions & 0 deletions problem3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Time Complexity : O(log m + log n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : yes
# Any problem you faced while coding this : No, it was a bit tricky to understand the problem statement and how to approach it, but once I got the idea, it was straightforward.



def search(self, reader: 'ArrayReader', target: int) -> int:
#this is to understand the range of the array, we will keep doubling the high until we find a value that is greater than the target
low = 0
high = 1
while reader.get(high) < target:
low = high
high = high * 2
#after this we will implement binary search on the range we found
while low <= high:
mid = low + (high - low) // 2
if reader.get(mid) == target:
return mid
if reader.get(low) <= reader.get(mid):
if reader.get(low) <= target < reader.get(mid):
high = mid - 1
else:
low = mid + 1
else:
if reader.get(mid) < target <= reader.get(high):
low = mid + 1
else:
high = mid - 1
return -1