forked from super30admin/Binary-Search-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem1.java
More file actions
70 lines (58 loc) · 1.96 KB
/
Problem1.java
File metadata and controls
70 lines (58 loc) · 1.96 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Time Complexity : O (log m + log n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No
// Your code here along with comments explaining your approach in three sentences only
/*
* I applied binary search first to find the correct row which may contain the element.
* Once I had the row with me, I used binary search on the row to find the element.
*/
public class Problem1 {
public boolean searchMatrix(int[][] matrix, int target) {
int n = matrix[0].length;
int rowToFind = searchRowRange(matrix, target);
if(rowToFind == -1) {
return false;
}
int low = 0;
int high = n-1;
while(low <= high) {
int mid = low + (high - low) / 2;
if(matrix[rowToFind][mid] == target) {
return true;
} else if(matrix[rowToFind][mid] > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return false;
}
private int searchRowRange(int[][] matrix, int target) {
int low = 0;
int high = matrix.length-1;
while(low <= high) {
int mid = low + (high - low) / 2;
if(matrix[mid][0] <= target && matrix[mid][matrix[0].length-1] >= target) {
return mid;
} else if(matrix[mid][0] > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
public static void main(String[] args) {
Problem1 sol = new Problem1();
int[][] matrix = {
{1, 3, 5, 7},
{10, 11, 16, 20},
{23, 30, 34, 60}
};
int target1 = 3;
int target2 = 13;
System.out.println(sol.searchMatrix(matrix, target1)); // Should print true
System.out.println(sol.searchMatrix(matrix, target2)); // Should print false
}
}