We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d9b3f38 commit 6aea026Copy full SHA for 6aea026
Medium/Count Servers That Communicate.java
@@ -0,0 +1,31 @@
1
+class Solution {
2
+ public int countServers(int[][] grid) {
3
+ if (grid.length == 0 || grid[0].length == 0) {
4
+ return 0;
5
+ }
6
+ int numRows = grid.length;
7
+ int numCols = grid[0].length;
8
+ int[] rowCount = new int[numRows];
9
+ int[] colCount = new int[numCols];
10
+ int numOfServers = 0;
11
+ for (int i = 0; i < numRows; i++) {
12
+ for (int j = 0; j < numCols; j++) {
13
+ if (grid[i][j] == 1) {
14
+ numOfServers++;
15
+ rowCount[i]++;
16
+ colCount[j]++;
17
18
19
20
21
22
23
+ if (rowCount[i] == 1 && colCount[j] == 1) {
24
+ numOfServers--;
25
26
27
28
29
+ return numOfServers;
30
31
+}
0 commit comments