-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1267-count-servers-communicate.dart
54 lines (46 loc) · 1.07 KB
/
1267-count-servers-communicate.dart
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
class Solution {
int countServers(List<List<int>> grid) {
final int m = grid.length;
final int n = grid[0].length;
List<int> rowCount = List.filled(m, 0);
List<int> colCount = List.filled(n, 0);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
rowCount[i]++;
colCount[j]++;
}
}
}
int result = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1 && (rowCount[i] > 1 || colCount[j] > 1)) {
result++;
}
}
}
return result;
}
}
// Test cases
void main() {
final solution = Solution();
final grid1 = [
[1, 0],
[0, 1]
];
print('Test case 1: ${solution.countServers(grid1)}'); // Expected: 0
final grid2 = [
[1, 0],
[1, 1]
];
print('Test case 2: ${solution.countServers(grid2)}'); // Expected: 3
final grid3 = [
[1, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
];
print('Test case 3: ${solution.countServers(grid3)}'); // Expected: 4
}