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 search_in_2d_matrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Time Complexity : log(mn)
// Space Complexity : O(mn)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : yes mid calcaution is a bit weird to wrap my head around as i ma seeing it for the first time

/*
* we are treating 2D matrix as a 1d matrix as in the problem statment is it given that last number of row will be smaller then the
* first number of the next row
*
* we are calculating mid as if it was a 1d array and after after words we are converting that mid to row and column index using row size
* divide by row size for row postion and modlus by row size to get column postion
*
* we will use standard binary search
*/

class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int left = 0;
int right = matrix[0].length * matrix.length - 1;
int n = matrix[0].length;
while (left <= right) {
int mid = left + (right - left) / 2;
int r = mid / n;
int c = mid % n;
if (matrix[r][c] == target) {
return true;
} else if (matrix[r][c] > target) {
right = mid - 1;
} else {
left = mid + 1;
}
}

return false;

}
}
25 changes: 25 additions & 0 deletions search_in_a_sorted_array_of_unknown_size.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public int search(ArrayReader reader, int target) {
int low = 0;
int high = 1;
while (reader.get(high) <= target) {
if (reader.get(high) == target) {
return high;
} else {
low = high;
high = high * 2;
}
}
while (low <= high) {
int mid = low + (high - low) / 2;
if (reader.get(mid) == target) {
return mid;
} else if (reader.get(mid) > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
}
34 changes: 34 additions & 0 deletions search_in_rotated_sorted_array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Time Complexity : log n
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : keeping in mind that every time we will get one side as sorted and one side a not was deficult

/*
* Approach
* standard binary search
* first we check check which side is sorted and then check if the target is in that side or not, if not check the other side
*/

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