LeetCode solutions

1704. Special Positions in a Binary Matrix

My accepted Python solution to LeetCode problem 1704, Special Positions in a Binary Matrix, running in 6ms.

  • Difficulty: Easy
  • Python
  • Runtime 6ms
  • Memory 17.7MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 6ms, memory 17.7MB, accepted 2025-12-31.

python
class Solution:
    def numSpecial(self, mat: List[List[int]]) -> int:
        m, n = len(mat), len(mat[0])
        row_sum = [sum(row) for row in mat]
        col_sum = [sum(mat[i][j] for i in range(m)) for j in range(n)]
        
        count = 0
        for i in range(m):
            for j in range(n):
                if mat[i][j] == 1 and row_sum[i] == 1 and col_sum[j] == 1:
                    count += 1
        return count

Source