1582. Special Positions in a Binary Matrix
UnknownView on LeetCode
Time: O(mn)
Space: O(m+n)
Problem Overview
Special Positions in a Binary Matrix (Unknown) asks you to solve a structured algorithmic task. This is a common Array / Matrix pattern in coding interviews. Precompute row and column sums; count cells that are 1 with row sum = col sum = 1.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
Precompute row and column sums; count cells that are 1 with row sum = col sum = 1.
1582.cs
C#
// Approach: Precompute row and column sums; count cells that are 1 with row sum = col sum = 1.
// Time: O(mn) Space: O(m+n)
public class Solution
{
public int NumSpecial(int[][] mat)
{
int m = mat.Length;
int n = mat[0].Length;
int[] rowsCount = new int[m];
int[] colsCount = new int[n];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (mat[i][j] == 1)
{
rowsCount[i]++;
colsCount[j]++;
}
}
}
int count = 0;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (mat[i][j] == 1 && rowsCount[i] == 1 && colsCount[j] == 1)
count++;
}
}
return count;
}
}Was this solution helpful?
Related Problems
- 4. Median of Two Sorted Arrays(Hard)
- 11. Container With Most Water(Medium)
- 15. 3Sum(Medium)
- 16. 3Sum Closest(Medium)
- 26. Remove Duplicates from Sorted Array(Easy)
- 27. Remove Element(Easy)