Problem statement

https://binarysearch.com/problems/Largest-Elements-in-Their-Row-and-Column/

Solution

Equal to Leetcode 0531. Lonely Pixel I.

Complexity

It is O(mn) for time and O(m + n) for space.

Code

class Solution:
    def solve(self, A):
        m, n, ans = len(A), len(A[0]), 0
        rows, cols = [0]*m, [0]*n

        for r, c in product(range(m), range(n)):
            rows[r] += A[r][c]
            cols[c] += A[r][c]

        for r, c in product(range(m), range(n)):
            ans += (rows[r] == 1) and (cols[c] == 1) and (A[r][c] == 1)

        return ans