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
32 changes: 32 additions & 0 deletions seach-infinite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Time Complexity : O(logn)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :No
// Any problem you faced while coding this :None

// Your code here along with comments explaining your approach in three sentences only
//double the space when target is not found
//move the low to high
//do binary search in the given space when the range is found. Adding a safety check so that the loop wont break and throw error.

class Solution {
public int search(ArrayReader reader, int target) {
int low = 0;
int high = 1;

while (reader.get(high) != Integer.MAX_VALUE && reader.get(high) <= target){
low = high;
high = 2* low;
}
while(low<=high){
int mid = low + (high-low)/2;
if (target == reader.get(mid)){
return mid;
}else if (target > reader.get(mid)){
low = mid + 1;
}else{
high = mid - 1;
}
}
return -1;
}
}
27 changes: 27 additions & 0 deletions search-infinite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Time Complexity : O(logn)
# Space Complexity :O(1)
# Did this code successfully run on Leetcode :No
# Any problem you faced while coding this :None

# Your code here along with comments explaining your approach in three sentences only
#double the space when target is not found
#move the low to high
#do binary search in the given space when the range is found. Adding a safety check so that the loop wont break and throw error.

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

while (reader.get(high) != 2**31 - 1 and reader.get(high) <= target):
low = high
high = 2* low
while(low<=high):
mid = low + (high-low)/2
if (target == reader.get(mid)):
return mid;
elif (target > reader.get(mid)):
low = mid + 1;
else:
high = mid - 1;
return -1;
44 changes: 44 additions & 0 deletions search-rotated.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Time Complexity : O(logn)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :yes
// Any problem you faced while coding this :None


// Your code here along with comments explaining your approach in three sentences only
//check if left sorted or right sorted, and then do binary search
//if left sorted, then check in left sorted array else in right sorted
//Do binary search for finding element in both

class Solution {
public int search(int[] nums, int target) {
int low = 0;
int high = nums.length-1;
while(low <= high){
int mid = low + (high-low)/2;
if(nums[low] <= nums[mid]){
if(target == nums[mid]){
return mid;
}
else if (target >= nums[low] && target < nums[mid]){
high = mid -1;
}
else{
low = mid + 1;
}

}else{
if(target == nums[mid]){
return mid;
}
else if (target > nums[mid] && target <= nums[high]){
low = mid + 1;
}
else{
high = mid - 1;
}
}
}
return -1;

}
}
33 changes: 33 additions & 0 deletions search-rotated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Time Complexity : O(logn)
# Space Complexity :O(1)
# Did this code successfully run on Leetcode :yes
# Any problem you faced while coding this :None


# Your code here along with comments explaining your approach in three sentences only
#check if left sorted or right sorted, and then do binary search
#if left sorted, then check in left sorted array else in right sorted
#Do binary search for finding element in both

class Solution:
def search(self, nums: List[int], target: int) -> int:
low = 0
high = len(nums)-1
while(low <= high):
mid = (high + low) #2
if(nums[low] <= nums[mid]):
if(target == nums[mid]):
return mid
elif (target >= nums[low] and target < nums[mid]):
high = mid -1
else:
low = mid + 1
else:
if(target == nums[mid]):
return mid
elif (target > nums[mid] and target <= nums[high]):
low = mid + 1
else:
high = mid - 1
return -1