- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1198.java
42 lines (40 loc) · 1.26 KB
/
_1198.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
packagecom.fishercoder.solutions.secondthousand;
publicclass_1198 {
publicstaticclassSolution1 {
publicintsmallestCommonElement(int[][] mat) {
intm = mat.length;
intn = mat[0].length;
for (intj = 0; j < n; j++) {
intminCommon = mat[0][j];
// we'll start from the second row
inti = 1;
for (; i < m; i++) {
if (thisRowHasThisNumber(mat[i], minCommon)) {
continue;
} else {
break;
}
}
if (i == m) {
returnminCommon;
}
}
return -1;
}
privatebooleanthisRowHasThisNumber(int[] nums, inttarget) {
intleft = 0;
intright = nums.length - 1;
while (left <= right) {
intmid = left + (right - left) / 2;
if (target == nums[mid]) {
returntrue;
} elseif (target > nums[mid]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
returnfalse;
}
}
}