Special Positions in a Binary Matrix (LeetCode 1582): Solution and Explanation

Easy
Array
Matrix

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.

Intuition

The direct way is to take each 1 and scan its whole row and column for another 1. That repeats the same scans many times when several 1s share a line.

The question "is this the only 1 in its row?" depends only on the row, not on the cell. Because the matrix is binary, the row sum is exactly the number of 1s in that row. The same holds for columns.

So compute every row sum and column sum once. Then a 1 at (i, j) is special exactly when row_sum[i] == 1 and col_sum[j] == 1, since the only 1 counted in each line must be this one.

Approach: Row and column sums

  1. Compute row_sum[i] for every row and col_sum[j] for every column.
  2. Set count = 0.
  3. For every cell with mat[i][j] == 1, check row_sum[i] == 1 and col_sum[j] == 1.
  4. If both hold, increment count.
  5. Return count.

Step-by-step example

mat = [[1,0,0],[0,0,1],[1,0,0]]
  1. Row sums are [1, 1, 1]. Column sums are [2, 0, 1].
  2. (0,0) is a 1, row_sum[0] = 1 but col_sum[0] = 2, so the 1 at (2,0) shares its column. Not special.
  3. (1,2) is a 1, row_sum[1] = 1 and col_sum[2] = 1. Special, count = 1.
  4. (2,0) is a 1, but col_sum[0] = 2 again. Not special.
  5. The function returns 1.

Code: Python, Java, C++

1class Solution:
2    def numSpecial(self, mat: List[List[int]]) -> int:
3        row_sum = [sum(row) for row in mat]
4        col_sum = [sum(col) for col in zip(*mat)]
5        count = 0
6        for r in range(len(mat)):
7            for c in range(len(mat[0])):
8                if mat[r][c] == 1 and row_sum[r] == 1 and col_sum[c] == 1:
9                    count += 1
10        return count
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(m x n)

One pass builds the sums, a second pass checks each cell in O(1).

Space

O(m + n)

One sum per row and one per column.

Other approaches

Scan the row and column of each 1 (time O(m x n x (m + n)), space O(1))

For every 1, walk its row and its column and make sure no other 1 appears. It needs no extra memory, but each check costs O(m + n), so a dense matrix takes O(m x n x (m + n)).

Only check rows with sum 1 (time O(m x n), space O(m + n))

After computing sums, skip every row whose sum is not 1; in the remaining rows, find the single 1 and test its column sum. Same complexity, slightly fewer checks.

Common mistakes

  • Checking only the row or only the column. A special position needs both to be clear.
  • Counting cells where both sums are 1 but the cell itself is 0. The row's single 1 and the column's single 1 may be at different cells.
  • Comparing the sums with 0 instead of 1. The sums include the cell's own 1.

Frequently asked questions

Why is checking the sums enough?

The matrix holds only 0s and 1s, so a row sum of 1 means exactly one 1 in that row. If the cell itself is 1, that single 1 must be this cell, and the same argument applies to the column.

Why must I also check mat[i][j] == 1?

Row i could have its only 1 at a different column, and column j its only 1 at a different row. Then both sums are 1 while mat[i][j] is 0.

Can there be more special positions than min(m, n)?

No. Each special position owns its row and column alone, so no two of them share a row or a column.

What is the complexity?

O(m x n) time and O(m + n) extra space.