Watch 10 video solutions for Special Positions in a Binary Matrix, a easy level problem involving Array, Matrix. This walkthrough by codestorywithMIK has 8,535 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given an m x n binary matrix mat, return the number of special positions in mat.
A position (i, j) is called special if mat[i][j] == 1 and all other elements in row i and column j are 0 (rows and columns are 0-indexed).
Example 1:
Input: mat = [[1,0,0],[0,0,1],[1,0,0]] Output: 1 Explanation: (1, 2) is a special position because mat[1][2] == 1 and all other elements in row 1 and column 2 are 0.
Example 2:
Input: mat = [[1,0,0],[0,1,0],[0,0,1]] Output: 3 Explanation: (0, 0), (1, 1) and (2, 2) are special positions.
Constraints:
m == mat.lengthn == mat[i].length1 <= m, n <= 100mat[i][j] is either 0 or 1.Problem Overview: You are given an m x n binary matrix. A position (i, j) is considered special if mat[i][j] == 1 and every other element in row i and column j is 0. The task is to count how many such positions exist in the matrix.
Approach 1: Naive Iteration Approach (O(m * n * (m + n)) time, O(1) space)
Scan every cell in the matrix. When you encounter a 1, verify whether it is the only 1 in its row and column. This requires iterating across the entire row and then the entire column to ensure no other 1 exists. If both checks pass, increment the result. The logic is straightforward and requires no extra memory, but the repeated row and column scans make it slower for larger matrices. This approach is useful when you want a quick brute-force implementation using basic array traversal.
Approach 2: Row and Column Preprocessing (O(m * n) time, O(m + n) space)
Instead of repeatedly scanning rows and columns, precompute the number of 1s in each row and each column. First pass: iterate through the matrix and store counts in two arrays, rowCount[m] and colCount[n]. Second pass: check every cell again. If mat[i][j] == 1 and rowCount[i] == 1 and colCount[j] == 1, the position is special. This removes redundant work and guarantees linear traversal of the matrix. The technique is a common optimization pattern when working with matrix problems where repeated row or column checks occur.
Recommended for interviews: The row and column preprocessing approach is the expected solution. Interviewers want to see that you recognize repeated scans and eliminate them with simple counting arrays. Mentioning the brute-force solution first demonstrates understanding of the problem constraints, but implementing the preprocessing optimization shows stronger problem-solving skills and awareness of time complexity improvements.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Naive Iteration | O(m * n * (m + n)) | O(1) | When writing a quick brute-force solution or when matrix size is very small |
| Row and Column Preprocessing | O(m * n) | O(m + n) | General case and interview-preferred approach that avoids repeated row/column scans |