haoel / leetcode

LeetCode Problems' Solutions
17.63k stars 4.92k forks source link

ValueEror Square submatrices #301

Open sgmmd opened 1 week ago

sgmmd commented 1 week ago

when i run this code i get an runtime error and it writes runtime:0ms

class Solution:
    def countSquares(self, matrix: list[list[int]]) -> int:
        self.MATRIXWIDTH,self.MATRIXHIGHT = len(matrix[0]),len(matrix)
        maximumBoundary = [-1,-1]
        boundaries= []
        for y,lines in enumerate(matrix):
            for x,cols in enumerate(lines):

                if cols == 0:

                    if x > maximumBoundary[0]:maximumBoundary[0] = x
                    if y > maximumBoundary[1]: maximumBoundary[1] = y
                    boundaries.append((x,y))

        amount = []

            # get the closest zero if the zero isn't pass the current position
        closestBoundary = lambda x, y: min(
            (max(bounds[0] - x, bounds[1] - y) for bounds in boundaries
             if bounds[0] >= x and bounds[1] >= y))

        value = 0
        for y,lines in enumerate(matrix):
            for x,cols in enumerate(lines):
                value=0
                if cols==0:continue
                if x > maximumBoundary[0] or y > maximumBoundary[1]:
                    value = min(self.MATRIXWIDTH-x,self.MATRIXHIGHT-y)
                else:

                    value = min(closestBoundary(x,y),min(self.MATRIXWIDTH-x,self.MATRIXHIGHT-y))

                amount.append(value)

        return sum(amount)

ValueError: min() arg is an empty sequence ^^^^ closestBoundry = lambda x, y: min( Line 19 in <lambda> (Solution.py) ^^^^^^^^^^^^^^^^^^^ print('hi',closestBoundry(x,y)+1,x,maximumBoundry) Line 34 in countSquares (Solution.py) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ret = Solution().countSquares(param_1) Line 65 in _driver (Solution.py) _driver() Line 80 in <module> (Solution.py)

UniKylin commented 1 week ago

Mr. Chen Hao has left us forever 🙏