# LeetCode: Special Positions in a Binary Matrix Solution

Count number of 1s

## Approach

Count number of 1s in each row, column

A special position is the position that

• has the value of 1
• number of 1s in its row and column are both 1

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}var numSpecial = function (mat) {2  const [m, n] = [mat.length, mat.length]3  const [numberOf1sInRow, numberOf1sInCol] = [4    new Uint8Array(m),5    new Uint8Array(n),6  ]7  let res = 08
9  for (let row = 0; row < m; row++) {10    for (let col = 0; col < n; col++) {11      if (mat[row][col]) {12        numberOf1sInRow[row]++13        numberOf1sInCol[col]++14      }15    }16  }17
18  for (let row = 0; row < m; row++) {19    for (let col = 0; col < n; col++) {20      res +=21        mat[row][col] &&22        numberOf1sInRow[row] === 1 &&23        numberOf1sInCol[col] === 124      // boolean coerce to 1 if true, 0 if false25    }26  }27
28  return res29}```

Original problem

N/A

leetcode

array

matrix

## Next Post

LeetCode: Partition Equal Subset Sum

Oct 17, 2022

Memoized recursion

Search Posts