0

Program that finds the maximal rectangle containing only 1's of a binary matrix with the maximal histogram problem.

I am trying to do some tests on a code

def maximalRectangle(self, matrix):
    if not matrix or not matrix[0]:
        return 0
    n = len(matrix[0])
    height = [0] * (n + 1)
    ans = 0
    for row in matrix:
        for i in range(n):
            height[i] = height[i] + 1 if row[i] == '1' else 0
        stack = [-1]
        for i in range(n + 1):
            while height[i] < height[stack[-1]]:
                h = height[stack.pop()]
                w = i - 1 - stack[-1]
                ans = max(ans, h * w)
            stack.append(i)
    return ans

# Driver Code  
if __name__ == '__main__': 
    matrix = [[0, 1, 0, 1],
         [0, 1, 0, 1],
         [0, 1, 1, 1],
         [1, 1, 1, 1]]

    print(maximalRectangle(matrix)) 

I get TypeError: maximalRectangle() missing 1 required positional argument: 'matrix' error

Yira
  • 23
  • 4
  • You defined the function like this: `def maximalRectangle(self, matrix)` - so it takes two formal parameters: `self` and `matrix`. But the call `maximalRectangle(matrix)` provides only one argument - for the first formal parameter `self`. The second formal parameter `matrix` remains undefined – ForceBru Jan 04 '20 at 14:05
  • @ForceBru I did notice that but stuck on defining the self if I want to print with ```maximalRectangle(self,matrix)``` – Yira Jan 04 '20 at 14:08
  • Is `self` used anywhere in your function? Doesn't look like it, so you can safely delete this parameter – ForceBru Jan 04 '20 at 14:14
  • @ForceBru I did try that but my code prints out 0 each time then – Yira Jan 04 '20 at 14:21

1 Answers1

0

Solved by removing self and changing the print statement to:

print(maximalRectangle([
  ["1","0","1","0","0"],
  ["1","1","1","1","1"],
  ["1","1","1","1","1"],
  ["1","0","0","1","0"]]))
Yira
  • 23
  • 4