Skip to content
LC-1582 Easy LeetCode

1582. Special Positions in a Binary Matrix

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 69% Topics: Array, Matrix
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n^2)
# Space: O(n)

class Solution(object):
    def numSpecial(self, mat):
        """
        :type mat: List[List[int]]
        :rtype: int
        """
        rows, cols = [0]*len(mat), [0]*len(mat[0])
        for i in xrange(len(rows)):
            for j in xrange(len(cols)):
                if mat[i][j]:
                    rows[i] += 1
                    cols[j] += 1
        result = 0
        for i in xrange(len(rows)):
            for j in xrange(len(cols)):
                if mat[i][j] == rows[i] == cols[j] == 1:
                    result += 1
        return result

Solution from kamyu104/LeetCode-Solutions · MIT