Skip to content

Commit 345447f

Browse files
add skeleton forc 1267
1 parent e0cc1d5 commit 345447f

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ _If you like this project, please leave me a star._ ★
1111
|1282|[Group the People Given the Group Size They Belong To](https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1282.java) | | | |Medium||
1212
|1281|[Subtract the Product and Sum of Digits of an Integer](https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1281.java) | | | |Easy||
1313
|1277|[Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1277.java) | | | |Medium||
14+
|1267|[Count Servers that Communicate](https://leetcode.com/problems/count-servers-that-communicate/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1267.java) | | | |Medium||
1415
|1266|[Minimum Time Visiting All Points](https://leetcode.com/problems/minimum-time-visiting-all-points/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1266.java) | | | |Easy||
1516
|1260|[Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1260.java) | | | [:tv:](https://www.youtube.com/watch?v=9hBcARSiU0s)|Easy||
1617
|1252|[Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1252.java) | O(m*n + k) | O(m*n) | |Easy||

src/main/java/com/fishercoder/solutions/_1267.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.fishercoder.solutions;
22

3+
import com.fishercoder.common.utils.CommonUtils;
4+
35
/**
46
* 1267. Count Servers that Communicate
57
*
@@ -20,7 +22,6 @@
2022
*
2123
* Example 3:
2224
* Input: grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]
23-
*
2425
* Output: 4
2526
* Explanation: The two servers in the first row can communicate with each other.
2627
* The two servers in the third column can communicate with each other. The server at right bottom corner can't communicate with any other server.
@@ -34,8 +35,30 @@
3435
* */
3536
public class _1267 {
3637
public static class Solution1 {
38+
/**credit: https://leetcode.com/problems/count-servers-that-communicate/discuss/436188/Java-or-Clean-And-Simple-or-Beats-100*/
3739
public int countServers(int[][] grid) {
38-
return -1;
40+
int m = grid.length;
41+
int n = grid[0].length;
42+
int[] rowCount = new int[m];
43+
int[] columnCount = new int[n];
44+
int total = 0;
45+
for (int i = 0; i < m; i++) {
46+
for (int j = 0; j < n; j++) {
47+
if (grid[i][j] == 1) {
48+
rowCount[i]++;
49+
columnCount[j]++;
50+
total++;
51+
}
52+
}
53+
}
54+
for (int i = 0; i < m; i++) {
55+
for (int j = 0; j < n; j++) {
56+
if (grid[i][j] == 1 && rowCount[i] == 1 && columnCount[j] == 1) {
57+
total--;
58+
}
59+
}
60+
}
61+
return total;
3962
}
4063
}
4164
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1267;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1267Test {
10+
private static _1267.Solution1 solution1;
11+
private static int[][] grid;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1267.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
grid = new int[][]{
21+
{1, 0},
22+
{0, 1}
23+
};
24+
assertEquals(0, solution1.countServers(grid));
25+
}
26+
27+
@Test
28+
public void test2() {
29+
grid = new int[][]{
30+
{1, 0},
31+
{1, 1}
32+
};
33+
assertEquals(3, solution1.countServers(grid));
34+
}
35+
36+
@Test
37+
public void test3() {
38+
grid = new int[][]{
39+
{1, 1, 0, 0},
40+
{0, 0, 1, 0},
41+
{0, 0, 1, 0},
42+
{0, 0, 0, 1}
43+
};
44+
assertEquals(4, solution1.countServers(grid));
45+
}
46+
47+
@Test
48+
public void test4() {
49+
grid = new int[][]{
50+
{1, 1, 0, 0},
51+
{1, 1, 1, 0},
52+
{0, 0, 1, 0},
53+
{0, 0, 0, 1}
54+
};
55+
assertEquals(6, solution1.countServers(grid));
56+
}
57+
58+
}

0 commit comments

Comments
 (0)