30:00
Given an m x n binary matrix mat, return the number of special positions in mat.
m x n
mat
A position (i, j) is special if mat[i][j] == 1 and every other element in row i and in column j is 0.
(i, j)
mat[i][j] == 1
i
j
0
m==mat.lengthm == \text{mat.length}m==mat.length
n==mat[i].lengthn == \text{mat}[i].\text{length}n==mat[i].length
1≤m,n≤1001 \leq m, n \leq 1001≤m,n≤100
mat[i][j]\text{mat}[i][j]mat[i][j] is either 000 or 111
Run code to see results.
A 1 is special when it is the only 1 in its row and the only 1 in its column. Count the 1s per row and per column first.
left = ones per row, top = ones per column; crosshair line turns green when it holds a single 1
special positions
test for this 1
row_sum == 1 and col_sum == 1
In a matrix of 0s and 1s, a position is special when it holds a 1 and every other cell in its row and in its column is 0. Return how many special positions there are.