Special Positions in a Binary Matrix
EASYDescription
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 either0or1.
Approaches
Checkout 2 different approaches to solve Special Positions in a Binary Matrix. Click on different approaches to view the approach and algorithm in detail.
Brute Force Iteration
This approach involves iterating through every single cell of the matrix. For each cell that contains a 1, we then perform a check to see if it qualifies as a "special" position. This check involves scanning the entire corresponding row and column to ensure all other elements are 0.
Algorithm
- Initialize
specialCount = 0. - Get matrix dimensions
m(rows) andn(columns). - For
ifrom0tom-1:- For
jfrom0ton-1:- If
mat[i][j] == 1:- Set a flag
isSpecial = true. - Check the row: For
kfrom0ton-1:- If
k != jandmat[i][k] == 1, setisSpecial = falseand break the loop.
- If
- If
isSpecialisfalse, continue to the nextj. - Check the column: For
kfrom0tom-1:- If
k != iandmat[k][j] == 1, setisSpecial = falseand break the loop.
- If
- If
isSpecialis stilltrue, incrementspecialCount.
- Set a flag
- If
- For
- Return
specialCount.
The algorithm proceeds as follows:
- Initialize a counter
specialCountto 0. - Iterate through each cell
(i, j)of the matrixmat, whereiis the row index andjis the column index. - If
mat[i][j]is1, we need to verify if it's a special position. - To do this, we check two conditions:
a. Row Check: Iterate through all elements in row
i. If we find another1(at a columnkwherek != j), then this position is not special. b. Column Check: Iterate through all elements in columnj. If we find another1(at a rowkwherek != i), then this position is not special. - If both the row and column checks pass (meaning no other
1s were found), it means(i, j)is a special position. IncrementspecialCount. - After checking all cells, the value of
specialCountis the result.
class Solution {
public int numSpecial(int[][] mat) {
int m = mat.length;
int n = mat[0].length;
int specialCount = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] == 1) {
boolean isSpecial = true;
// Check row i
for (int k = 0; k < n; k++) {
if (k != j && mat[i][k] == 1) {
isSpecial = false;
break;
}
}
if (!isSpecial) continue;
// Check column j
for (int k = 0; k < m; k++) {
if (k != i && mat[k][j] == 1) {
isSpecial = false;
break;
}
}
if (isSpecial) {
specialCount++;
}
}
}
}
return specialCount;
}
}
Complexity Analysis
Pros and Cons
- Simple to understand and implement.
- Requires no extra space beyond the input matrix.
- Highly inefficient due to redundant computations. The same row or column is scanned multiple times for different
1s within them.
Code Solutions
Checking out 3 solutions in different languages for Special Positions in a Binary Matrix. Click on different languages to view the code.
class Solution {
public
int numSpecial(int[][] mat) {
int m = mat.length, n = mat[0].length;
int[] r = new int[m];
int[] c = new int[n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
r[i] += mat[i][j];
c[j] += mat[i][j];
}
}
int ans = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (mat[i][j] == 1 && r[i] == 1 && c[j] == 1) {
++ans;
}
}
}
return ans;
}
}
Video Solution
Watch the video walkthrough for Special Positions in a Binary Matrix
Similar Questions
5 related questions you might find useful
Data Structures:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.