JavaScript DP O(n) Time & Space
\n/**\n * @param {character[][]} matrix\n * @return {number}\n */\nconst maximalRectangle = (matrix) => {\n  if (!matrix.length || !matrix[0].length) return 0;\
```\n/**\n * @param {character[][]} matrix\n * @return {number}\n */\nconst maximalRectangle = (matrix) => {\n  if (!matrix.length || !matrix[0].length) return 0;\n  const height = matrix.length;\n  const width = matrix[0].length;\n  const lefts = matrix[0].map(() => 0);\n  const rights = matrix[0].map(() => width);\n  const heights = lefts.slice();\n  let max = 0;\n  for (let row = 0; row < height; row++) {\n    let left = 0;\n    let right = width;\n    for (let i = 0; i < width; i++) {\n      if (matrix[row][i] === \'1\') {\n        lefts[i] = Math.max(left, lefts[i]);\n        heights[i]++;\n      } else {\n        lefts[i] = heights[i] = 0;\n        left = i + 1\n      }\n\n      const rightIdx = width - 1 - i;\n      if (matrix[row][rightIdx] === \'1\') {\n        rights[rightIdx] = Math.min(right, rights[rightIdx])\n      } else {\n        rights[rightIdx] = width;\n        right = rightIdx;\n      }\n    }\n    for (let i = 0; i < width; i++) {\n      max = Math.max(max,(rights[i] - lefts[i]) * heights[i]);\n    }\n  }\n  return max\n}\n```
['JavaScript']