Leetcode 1704 Solution

This article provides solution to leetcode question 1704 (special-positions-in-a-binary-matrix)

https://leetcode.com/problems/special-positions-in-a-binary-matrix

Solution

class Solution:
    def numSpecial(self, mat: List[List[int]]) -> int:
        m = len(mat)
        n = len(mat[0])

        rows = [0] * m
        cols = [0] * n

        for i in range(m):
            for j in range(n):
                rows[i] += mat[i][j]
                cols[j] += mat[i][j]

        ans = 0
        for i in range(m):
            for j in range(n):
                if mat[i][j] and rows[i] == 1 and cols[j] == 1:
                    ans += 1
        return ans