Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bd55005

Browse files
add 1198
1 parent ca52c66 commit bd55005

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ _If you like this project, please leave me a star._ ★
1515
|1213|[Intersection of Three Sorted Arrays](https://leetcode.com/problems/intersection-of-three-sorted-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1213.java) | | | [:tv:](https://www.youtube.com/watch?v=zceoOrHSHNQ)|Easy||
1616
|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1207.java) | O(n) | O(1) | |Easy||
1717
|1200|[Minimum Absolute Difference](https://leetcode.com/problems/minimum-absolute-difference/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1200.java) | || [:tv:](https://www.youtube.com/watch?v=mH1aEjOEjcQ)|Easy||
18+
|1198|[Find Smallest Common Element in All Rows](https://leetcode.com/problems/find-smallest-common-element-in-all-rows/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1198.java) | | | |Easy||
1819
|1185|[Day of the Week](https://leetcode.com/problems/day-of-the-week/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1185.java) | | | |Easy||
1920
|1165|[Single-Row Keyboard](https://leetcode.com/problems/single-row-keyboard/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1165.java) | | | |Easy||
2021
|1160|[Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1160.java) | O(n) | O(m) | |Easy||
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1198. Find Smallest Common Element in All Rows
5+
*
6+
* Given a matrix mat where every row is sorted in increasing order, return the smallest common element in all rows.
7+
* If there is no common element, return -1.
8+
*
9+
* Example 1:
10+
* Input: mat = [[1,2,3,4,5],[2,4,5,8,10],[3,5,7,9,11],[1,3,5,7,9]]
11+
* Output: 5
12+
*
13+
* Constraints:
14+
* 1 <= mat.length, mat[i].length <= 500
15+
* 1 <= mat[i][j] <= 10^4
16+
* mat[i] is sorted in increasing order.
17+
* */
18+
public class _1198 {
19+
public static class Solution1 {
20+
public int smallestCommonElement(int[][] mat) {
21+
int m = mat.length;
22+
int n = mat[0].length;
23+
int minCommon;
24+
for (int j = 0; j < n; j++) {
25+
minCommon = mat[0][j];
26+
int i = 1;
27+
for (; i < m; i++) {//we'll start from the second row
28+
if (thisRowHasThisNumber(mat[i], minCommon)) {
29+
continue;
30+
} else {
31+
break;
32+
}
33+
}
34+
if (i == m) {
35+
return minCommon;
36+
}
37+
38+
}
39+
return -1;
40+
}
41+
42+
private boolean thisRowHasThisNumber(int[] nums, int target) {
43+
int left = 0;
44+
int right = nums.length - 1;
45+
while (left <= right) {
46+
int mid = left + (right - left) / 2;
47+
if (target == nums[mid]) {
48+
return true;
49+
} else if (target > nums[mid]) {
50+
left = mid + 1;
51+
} else {
52+
right = mid - 1;
53+
}
54+
}
55+
return false;
56+
}
57+
}
58+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1198;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1198Test {
10+
private static _1198.Solution1 solution1;
11+
private static int[][] mat;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1198.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
mat = new int[][]{
21+
{1, 2, 3, 4, 5},
22+
{2, 4, 5, 8, 10},
23+
{3, 5, 7, 9, 11},
24+
{1, 3, 5, 7, 9}
25+
};
26+
assertEquals(5, solution1.smallestCommonElement(mat));
27+
}
28+
29+
}

0 commit comments

Comments
 (0)