forked from super30admin/Binary-Search-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchIn2dMatrix.java
More file actions
29 lines (26 loc) · 1.05 KB
/
Copy pathSearchIn2dMatrix.java
File metadata and controls
29 lines (26 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Time Complexity : O(log m*n) as it's binary search on the complete matrix size.
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Not much, once the idea to consider it as a single array and perform the binary search to achieve O(log m*n) it was
// better. The core part of the solution lies in identifying the position of mid element.
// Your code here along with comments explaining your approach
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int low = 0;
int high = (matrix.length * matrix[0].length) - 1;
while(low <= high){
int mid = low + (high-low)/2;
int column = mid%matrix[0].length;
int row = mid/matrix[0].length;
if(matrix[row][column] == target){
return true;
}
if(matrix[row][column] > target){
high = mid - 1;
} else{
low = mid + 1;
}
}
return false;
}
}