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
30 changes: 30 additions & 0 deletions problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0) {
return false;
}
int m = matrix.length;
int n = matrix[0].length;
int low = 0;
int high = m*n-1;
while (low <= high) {
int mid = low + (high - low) / 2;
int row = mid / n;
int col = mid % n;
if (matrix[row][col] == target) {
return true;
}
else if (matrix[row][col] > target) {
high = mid - 1;
}
else {
low = mid + 1;
}
}
return false;
}
}
24 changes: 24 additions & 0 deletions problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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 :
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
if matrix == None or len(matrix) == 0:
return False
m = len(matrix)
n = len(matrix[0])
low = 0
high = m*n-1
while low <= high:
mid = low + (high - low) // 2
row = mid // n
col = mid % n
print("row = ", row , "col = ",col)
if matrix[row][col] == target:
return True
elif matrix[row][col] > target:
high = mid - 1
else:
low = mid + 1
return False
40 changes: 40 additions & 0 deletions problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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 {
public int search(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return -1;
}
int n = nums.length;
int low = 0;
int high = n - 1;

while (low <= high) {
int mid = low + (high - low) / 2; //prevent integer overflow

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

}
return -1;
}
}
28 changes: 28 additions & 0 deletions problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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:
def search(self, nums: List[int], target: int) -> int:
if nums == None and len(nums) == 0:
return -1
n = len(nums)
low = 0
high = n-1

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

if target == nums[mid]:
return mid;
elif nums[low] <= nums[mid]:
if target >= nums[low] and target <= nums[mid]:
high = mid-1
else:
low = mid+1
else:
if target > nums[mid] and target <= nums[high]:
low = mid+1
else:
high = mid-1
return -1
37 changes: 37 additions & 0 deletions problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 {
public int search (ArrayReader reader, int target) {
int low = 0;
int high = 1;

while (reader.get(high) < target) {
low = high;
high = high * 2;
}

if (reader.get(high) == target) {
return high;
}

return binary_search(reader, low, high, target);
}

public int binary_search(ArrayReader reader, int low, int high, int target) {
while (low <= high) {
int mid = low + (high - low) / 2;

if (reader.get(mid) == target) {
return mid;
} else if (reader.get(mid) < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}

return -1;
}
}
27 changes: 27 additions & 0 deletions problem3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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:
def search(self, reader: 'ArrayReader', target: int) -> int:
low = 0
high = 1

while reader.get(high) < target:
low = high
high = high * 2
if reader.get(high) == target:
return high

return self.binary_search(reader, low, high, target)

def binary_search(self, reader: 'ArrayReader', low: int, high: int, target: int) -> int:
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