Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1126 from supremvanam/main
Browse files Browse the repository at this point in the history
Update: 74-Search-A-2D-Matrix.java
  • Loading branch information
Ahmad-A0 authored Sep 17, 2022
2 parents a445a9b + 0dbe5bb commit 6827137
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions java/74-Search-A-2D-Matrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,28 @@ public boolean searchMatrix(int[][] matrix, int target) {
}
return false;
}

// Time: O(log(mn)) | Space: O(1)
public boolean searchMatrix2(int[][] matrix, int target) {
if(matrix.length == 0) return false;

int rows = matrix.length;
int columns = matrix[0].length;

int low = 0;
int high = rows * columns;

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

if(matrix[mid/columns][mid%columns] == target) {
return true;
} else if (matrix[mid/columns][mid%columns] < target) {
low = mid+1;
} else {
high = mid;
}
}
return false;
}
}

0 comments on commit 6827137

Please sign in to comment.