Skip to content

Commit d282f5f

Browse files
authored
Create special-positions-in-a-binary-matrix.cpp
1 parent 9e1dcde commit d282f5f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Time: O(n^2)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
int numSpecial(vector<vector<int>>& mat) {
7+
vector<int> rows(size(mat)), cols(size(mat[0]));
8+
for (int i = 0; i < size(rows); ++i) {
9+
for (int j = 0; j < size(cols); ++j) {
10+
if (mat[i][j]) {
11+
++rows[i], ++cols[j];
12+
}
13+
}
14+
}
15+
int result = 0;
16+
for (int i = 0; i < size(rows); ++i) {
17+
for (int j = 0; j < size(cols); ++j) {
18+
if (mat[i][j] && rows[i] == 1 && cols[j] == 1) {
19+
++result;
20+
}
21+
}
22+
}
23+
return result;
24+
}
25+
};

0 commit comments

Comments
 (0)