Python beats 99% using dictionary


  • 0
    class Solution(object):
        def findLonelyPixel(self, picture):
            dict1 = {}
            dict2 = {}
            matrix = []
            for i in range(len(picture)):
                for j in range(len(picture[0])):
                    if picture[i][j] == "B":
                        if i in dict1:
                            dict1[i] += 1
                        else:
                            dict1[i] = 1
                        if j in dict2:
                            dict2[j] += 1
                        else:
                            dict2[j] = 1
                        matrix.append([i,j])
            sum = 0
            for k in range(len(matrix)):
                if dict1[matrix[k][0]] == 1 and dict2[matrix[k][1]] == 1:
                    sum += 1
            return sum
    

Log in to reply
 

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