Python simple accepted solution


  • 0
    N

    It seems the top voted Java solution will get TLE in Python. I modified a bit and got accepted.

    def countCornerRectangles(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """        
    
        ret = 0
        dict = {}
    
        for row in grid:
            for i in range(len(row)):
                if row[i]:
                    for j in range(i+1, len(row)):
                        if row[j]:
                            dict[(i,j)] = dict.get((i,j), 0) + 1
    
        for count in dict.values():
            ret += count * (count - 1) // 2
    
        return ret
    

Log in to reply
 

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