549ms Python Code with NumPy


  • 0
    X

    Similar to @ALEXANDER 's solution. Take advantage of dot method in NumPy to count how many 1s in the same column between two distinct rows:

    import numpy as np
    import itertools
    
    class Solution(object):
        def countCornerRectangles(self, grid):
            """
            :type grid: List[List[int]]
            :rtype: int
            """
            m=np.array(grid)
            counter=0
            for (y0,y1) in itertools.combinations(m,2):
                ones=np.dot(y0,y1)
                counter+=ones*(ones-1)/2
            return int(counter)
    

  • 1

    Could use y and *y:

            for y in itertools.combinations(m,2):
                ones=np.dot(*y)
    

    And I can't resist...

    def countCornerRectangles(self, grid):
        return sum(sum(range(np.dot(*y))) for y in itertools.combinations(np.array(grid), 2))

Log in to reply
 

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