Special Positions in a Binary Matrix

Given an `m x n` binary matrix `mat`, return the number of special positions in `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`.

30:00

Special Positions in a Binary Matrix
easy
Free written solution
Topics
Companies

Given an m x n binary matrix mat, return the number of special positions in 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.

Example 1:
Input: {"mat":[[1,0,0],[0,0,1],[1,0,0]]}
Output: 1
Constraints:
  • m==mat.lengthm == \text{mat.length}

  • n==mat[i].lengthn == \text{mat}[i].\text{length}

  • 1≤m,n≤1001 \leq m, n \leq 100

  • mat[i][j]\text{mat}[i][j] is either 00 or 11

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"mat":[[1,0,0],[0,0,1],[1,0,0]]}

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

0

test for this 1

row_sum == 1 and col_sum == 1

Variables
VariableValue
count0
DepthFunction Call
Stack empty
0/11