1
# Runtime: 1058 ms (Top 6.78%) | Memory: 15.2 MB (Top 89.63%)3
def maxArea(self, arr: List[List[int]]) -> int:4
maxA = 0 # local max variable to return max area of a 1D array7
] # initializing with minimum value so we can avoid using loop for remaining element in the stack after whole traversing10
) # value for stack index -1 and one extra iteration to empty the stack11
for i in range(len(arr)): # traversing for stack from left to right12
arr[i] = int(arr[i]) # we are getting str value from matrix13
while stack and arr[stack[-1]] >= int(15
): # stack is non empty and stack top is greater or equal to current element16
index = stack.pop() # pop greater element from stack and finds its area18
i - stack[-1] - 1 if stack else i19
) # for empty stack width is len of 1D arr, and after poping if we have element in stack then it becomes left boundary and current index as right boundary20
maxA = max(maxA, width * arr[index]) # geting max area (L * B)21
stack.append(int(i)) # inserting each element in stack22
return maxA # return the max area24
"""Below is the code for adding each row one by one and passing it to above function to get the max area for each row"""27
# r = 2 0 2 1 1 This is for else condition31
# r = 2 0 2 1 0 This is for if condition33
def maximalRectangle(self, matrix: List[List[str]]) -> int:34
result = [0] * len(matrix[0])36
maxReact = self.maxArea(result)37
for i in range(len(matrix)):38
for j in range(len(matrix[i])):39
matrix[i][j] = int(matrix[i][j])43
result[j] = result[j] + matrix[i][j]45
maxReact, self.maxArea(result)46
) # after getting max area for 1D arr we are getting max value from those 1D arr for the 2D arr