-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathSearcha2DMatrix.java
160 lines (108 loc) · 3.18 KB
/
Searcha2DMatrix.java
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
Source: https://leetcode.com/problems/search-a-2d-matrix/submissions/
3 Approaches
1st Approach
Basic Idea:
Check each row and if target is less than or equal to the last element of any row, then do binary search on elements of that row
Time: O(m), more specifically (O(m + log₂(n))) where m is the number of rows and n is the number of columns
Space: O(1), constant space
*/
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]) {
return isTargetPresent(matrix[i], target);
}
}
return false;
}
private boolean isTargetPresent(int[] row, int target) {
int start = 0;
int end = row.length - 1;
while(start <= end) {
int mid = (start + end) >> 1;
if(row[mid] == target) {
return true;
}
if(target > row[mid]) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return false;
}
}
--------------------------------------------------------------------------------------------
/*
2nd Approach
Basic Idea:
First do binary search on rows and if there is a possibility of having target in a row, then do binary search on elements of that row
Time: O(log₂(m * n)), more specifically (O(log₂(m) + log₂(n))) where m is the number of rows and n is the number of columns
Space: O(1), constant space
*/
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int top = 0;
int down = matrix.length - 1;
int n = matrix[0].length - 1;
while(top <= down) {
int mid = (top + down) >> 1;
// log(m) to search for the row having possibility of containing target
if(target >= matrix[mid][0] && target <= matrix[mid][n]) {
return isTargetPresent(matrix[mid], target); // log(n) to search for the target in that row
}
if(target > matrix[mid][n]) {
top = mid + 1;
} else {
down = mid - 1;
}
}
return false;
}
private boolean isTargetPresent(int[] row, int target) {
int start = 0;
int end = row.length - 1;
while(start <= end) {
int mid = (start + end) >> 1;
if(row[mid] == target) {
return true;
}
if(target > row[mid]) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return false;
}
}
--------------------------------------------------------------------------------------------
/*
Approach 3 (Treat as 1D sorted array)
Time: O(log₂(m * n)), where m is the number of rows and n is the number of columns
Space: O(1), constant space
*/
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length;
int n = matrix[0].length;
int start = 0;
int end = m * n - 1;
while(start <= end) {
int mid = (start + end) >> 1;
int currElement = matrix[mid / n][mid % n]; // [mid / n] to get row and [mid % n] to get column
if(currElement == target) {
return true;
}
if(target > currElement) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return false;
}
}