Python solution with detailed explanation


  • 0
    G

    Solution

    Lonely Pixel I https://leetcode.com/problems/lonely-pixel-i/#/description

    from collections import defaultdict
    class Solution(object):
        def findLonelyPixel(self, picture):
            """
            :type picture: List[List[str]]
            :rtype: int
            """
            if picture == []:
                return
            self.rows, self.cols = defaultdict(int), defaultdict(int)
            items = []
            N,M = len(picture), len(picture[0])
            for i in range(N):
                for j in range(M):
                    if picture[i][j] == "B":
                        self.rows[i] += 1
                        self.cols[j] += 1
                        items.append((i,j))
            cnt = 0
            for i,j in items:
                if i in self.rows and self.rows[i] == 1 and j in self.cols and self.cols[j] == 1:
                    cnt += 1
            return cnt
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.