forked from super30admin/Binary-Search-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem3.java
More file actions
77 lines (72 loc) · 2.38 KB
/
Problem3.java
File metadata and controls
77 lines (72 loc) · 2.38 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
71
72
73
74
75
76
77
//Problem 3: Search a 2D Matrix
// 1st Approach:
// Considering matrix as imaginary 1D array and doing binary search on it
// This approach treats the matrix as a single sorted list.
// Mapping 1D index to 2D: row = index / columns, col = index % columns.
class Solution {
// Time Complexity: O(log(m) + log(n)) = O(log(mn))
//Space Complexity: O(1)
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length;
int n = matrix[0].length;
int low = 0;
int high = m * n - 1;
while(low <= high){
int mid = low + (high - low)/2; // to avoid integer overflow
int r = mid / n;
int c = mid % n;
if(matrix[r][c] == target){
return true;
}
if(matrix[r][c] > target){
high = r * n + c - 1;
}else{
low = r * n + c + 1;
}
}
return false;
}
}
// 2nd approach:
// Doing binary search on row first to get the target's possible row. Then perform binary search on columns to search the target
// This is a two-step approach:
// 1. Find the potential row where the target could exist.
// 2. Perform a standard binary search within that specific row.
class Solution2 {
// Time Complexity: O(log(m) + log(n)) = O(log(mn))
//Space Complexity: O(1)
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length;
int n = matrix[0].length;
int low = 0;
int high = m - 1;
int mid = 0;
// perform binary search on rows to get the range
while(low <= high){
mid = low + (high - low)/2;
if(matrix[mid][0] <= target && matrix[mid][n-1] >= target){
break;
}
else if(matrix[mid][0] > target && matrix[0][0] <= target){
high = mid - 1;
}else{
low = mid + 1;
}
}
low = 0;
high = n - 1;
int row = mid;
// perform second binary search to find the target
while(low <= high){
mid = low + (high - low)/2;
if(matrix[row][mid] == target){
return true;
}
else if(matrix[row][mid] > target){
high = mid - 1;
}else{
low = mid + 1;
}
}
return false;
}