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
76 changes: 76 additions & 0 deletions Leetcode_33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//https://www.youtube.com/watch?v=u2LQVywF-H0&feature=youtu.be
//https://www.thes30.com/problem/5c910dbe6b94da0016a57018/solutions
//Identify mid point. Either of the side will be sorted array. Check if the given target is with in sorted array. If yes, search in that sorted array. Else, search in another side i.e., unsorted array

//TC: O(logn)
//SC: O(1)

class Solution {
public int search(int[] nums, int target) {
int low=0,high=nums.length-1;

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

if(nums[mid]==target) return mid;

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

if(nums[low]==target){
return low;
}

return -1;
}
}



//Approach-2
//https://www.youtube.com/watch?v=u2LQVywF-H0&feature=youtu.be
//https://www.thes30.com/problem/5c910dbe6b94da0016a57018/solutions
//Identify mid point. Either of the side will be sorted array. Check if the given target is with in sorted array. If yes, search in that sorted array. Else, search in another side i.e., unsorted array

//TC: O(logn)
//SC: O(1)

class Solution {
public int search(int[] nums, int target) {
int low=0,high=nums.length-1;

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

if(nums[mid]==target) return mid;

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;
}
}
41 changes: 41 additions & 0 deletions Leetcode_702
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* // This is ArrayReader's API interface.
* // You should not implement it, or speculate about its implementation
* interface ArrayReader {
* public int get(int index) {}
* }
*/
//Travel to a index till we find the value at that particular index is greater than target by making low to high and increasing high value to twice every time. Then do a binary search on reader from low to high to find the target.
//TC: O(M)+O(Log m)
//SC: O(1)
class Solution {
public int search(ArrayReader reader, int target) {
int low=0;
int high=1;

while(true){
if(reader.get(high)==target){
return high;
}else if(reader.get(high)>target){
break;
}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;


}
}
121 changes: 121 additions & 0 deletions Leetcode_74.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//Way 1
//Linear Traversal
//TC: O(m*n); SC:O(1)
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m=matrix.length;
int n=matrix[0].length;

for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(matrix[i][j]==target){
return true;
}
}
}

return false;
}
}

//Way 2
//Travel every row. Check last element. If last element is less than or equal to target, check that respective row. If target is found, return true.
//TC: O(m+n); SC:O(1)
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m=matrix.length;
int n=matrix[0].length;

for(int i=0;i<m;i++){
if(target<=matrix[i][n-1]){

for(int j=0;j<n;j++){
if(matrix[i][j]==target){
return true;
}
}
return false;
}
}

return false;
}
}

//Way 3
//Consider matrix as imaginary sorted array. Perform binary search on it and convert the mid to it's respective row and col. Check whether element at position is target or not and continue the binary search
//TC: log m+log n; SC:O(1)
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m=matrix.length;
int n=matrix[0].length;

int low=0,high=m*n-1;

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

int r=mid/n;
int c=mid%n;

if(matrix[r][c]==target){
return true;
}else if(matrix[r][c]<target){
low=mid+1;
}else{
high=mid-1;
}
}

return false;

}
}

//way 4
//Identify the row using binary search. Identify the presence of target in that row using binary saerch
//TC: log m+log n; SC:O(1)
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m=matrix.length;
int n=matrix[0].length;

//Identify row:
int l=0,h=m-1;

while(l<=h){
int mid=l+(h-l)/2;

if(matrix[mid][n-1]==target){
return true;
} else if(matrix[mid][n-1]>target){
h=mid-1;
}else{
l=mid+1;
}
}

if(l>=m) return false;

return binarySearch(matrix[l],target);

}

private boolean binarySearch(int[] row,int target){
int low=0,high=row.length-1;

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

if(row[mid]==target){
return true;
}else if(row[mid]>target){
high=mid-1;
}else{
low=mid+1;
}
}

return false;
}
}