All files / leetCode 0085.ts

100% Statements 17/17
100% Branches 8/8
100% Functions 2/2
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26      6x 1x   5x 5x 8x     5x 8x 26x 16x       5x   5x 5x   5x    
import largestRectangleArea from './0084'
 
export default function maximalRectangle(matrix: string[][]): number {
  if (matrix.length === 0 || matrix[0].length === 0)
    return 0
 
  const m = matrix.length
  const n = matrix[0].length
  const heights = Array.from<number>({ length: m }).map(() => Array.from<number>({ length: n }).fill(0))
 
  // Use dynamic programming to calculate heights of `m` histograms
  for (let i = 0; i < m; i++) {
    for (let j = 0; j < n; j++) {
      if (matrix[i][j] === '1')
        heights[i][j] = i === 0 ? 1 : heights[i - 1][j] + 1
    }
  }
 
  let res = 0
 
  for (let i = 0; i < m; i++)
    res = Math.max(res, largestRectangleArea(heights[i]))
 
  return res
}