2026-03-04 20:10:10 +0300 MSK

Special Positions in a Binary Matrix

Code

class Solution:
    def numSpecial(self, mat: List[List[int]]) -> int:
        m = len(mat)
        n = len(mat[0])
        row_count = [0] * m
        col_count = [0] * n
        ones = []
        for row in range(m):
            for col in range(n):
                if mat[row][col] == 1:
                    row_count[row] += 1
                    col_count[col] += 1
                    ones.append((row, col))
        res = 0
        while ones:
            row, col = ones.pop()
            if row_count[row] == 1 and col_count[col] == 1:
                res += 1
        return res