Skip to the code
cf.
+50 XP

#66 Hottest Rectangle in a Heatmap

hard

A product team views a grid of net engagement changes per screen region in grid. They want to highlight the rectangle of neighboring cells with the largest total.

Write hottestRectangle(grid) that returns the largest total of any rectangle of at least one cell.

Example 1

Input: grid = [[1,-2,3],[-1,4,-5],[2,1,-1]]
Output: 6

The 2 by 2 block [[-1, 4], [2, 1]] in the bottom-left totals 6.

Example 2

Input: grid = [[-3,-1],[-4,-2]]
Output: -1

Every cell is negative, so the best rectangle is the single cell -1.

Constraints

  • 1 <= rows, cols <= 100
  • -1000 <= grid[r][c] <= 1000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).