-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem_1267_countServers.cc
60 lines (55 loc) · 1.05 KB
/
Problem_1267_countServers.cc
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
55
56
57
58
59
60
#include <cmath>
#include <iostream>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
int countServers(vector<vector<int>> &grid)
{
int n = grid.size();
int m = grid[0].size();
vector<int> row(n);
vector<int> col(m);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (grid[i][j] == 1)
{
row[i]++;
col[j]++;
}
}
}
int ans = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (grid[i][j] == 1 && (row[i] > 1 || col[j] > 1))
{
ans++;
}
}
}
return ans;
}
};
void test()
{
Solution s;
vector<vector<int>> g1 = {{1, 0}, {0, 1}};
vector<vector<int>> g2 = {{1, 0}, {1, 1}};
vector<vector<int>> g3 = {{1, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
EXPECT_EQ_INT(0, s.countServers(g1));
EXPECT_EQ_INT(3, s.countServers(g2));
EXPECT_EQ_INT(4, s.countServers(g3));
EXPECT_SUMMARY;
}
int main()
{
test();
return 0;
}