| from typing import List | |
| class Solution: | |
| def maximalRectangle(self, matrix: List[List[str]]) -> int: | |
| if not matrix or not matrix[0]: | |
| return 0 | |
| m, n = len(matrix), len(matrix[0]) | |
| height = [0] * n | |
| left = [-1] * n | |
| right = [n] * n | |
| res = 0 | |
| for i in range(m): | |
| cur_left, cur_right = -1, n | |
| for j in range(n): | |
| if matrix[i][j] == '1': | |
| height[j] += 1 | |
| else: | |
| height[j] = 0 | |
| for j in range(n): | |
| if matrix[i][j] == '1': | |
| left[j] = max(left[j], cur_left) | |
| else: | |
| left[j] = -1 | |
| cur_left = j | |
| for j in range(n-1, -1, -1): | |
| if matrix[i][j] == '1': | |
| right[j] = min(right[j], cur_right) | |
| else: | |
| right[j] = n | |
| cur_right = j | |
| for j in range(n): | |
| res = max(res, height[j] * (right[j] - left[j] - 1)) | |
| return res | |