Special Positions in a Binary Matrix (LeetCode 1582): Solution and Explanation
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
- Compute
row_sum[i]for every row andcol_sum[j]for every column. - Set
count = 0. - For every cell with
mat[i][j] == 1, checkrow_sum[i] == 1andcol_sum[j] == 1. - If both hold, increment
count. - Return
count.
Step-by-step example
mat = [[1,0,0],[0,0,1],[1,0,0]]
- Row sums are [1, 1, 1]. Column sums are [2, 0, 1].
- (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.
- (1,2) is a 1, row_sum[1] = 1 and col_sum[2] = 1. Special, count = 1.
- (2,0) is a 1, but col_sum[0] = 2 again. Not special.
- 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 count1class Solution {
2 public int numSpecial(int[][] mat) {
3 int m = mat.length, n = mat[0].length;
4 int[] rowSum = new int[m];
5 int[] colSum = new int[n];
6 for (int r = 0; r < m; r++) {
7 for (int c = 0; c < n; c++) {
8 rowSum[r] += mat[r][c];
9 colSum[c] += mat[r][c];
10 }
11 }
12 int count = 0;
13 for (int r = 0; r < m; r++) {
14 for (int c = 0; c < n; c++) {
15 if (mat[r][c] == 1 && rowSum[r] == 1 && colSum[c] == 1) {
16 count++;
17 }
18 }
19 }
20 return count;
21 }
22}1class Solution {
2public:
3 int numSpecial(vector<vector<int>>& mat) {
4 int m = mat.size(), n = mat[0].size();
5 vector<int> rowSum(m, 0), colSum(n, 0);
6 for (int r = 0; r < m; r++) {
7 for (int c = 0; c < n; c++) {
8 rowSum[r] += mat[r][c];
9 colSum[c] += mat[r][c];
10 }
11 }
12 int count = 0;
13 for (int r = 0; r < m; r++) {
14 for (int c = 0; c < n; c++) {
15 if (mat[r][c] == 1 && rowSum[r] == 1 && colSum[c] == 1) {
16 count++;
17 }
18 }
19 }
20 return count;
21 }
22};Time and space complexity
O(m x n)
One pass builds the sums, a second pass checks each cell in O(1).
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.