Parcel-delivery app~20 min2s · 256 MBtarget O(R × C) time, O(C) spacetraversal
You work on a delivery network that keeps a map of each warehouse floor. The map is a grid of letters, and each letter names the product stored in that cell. The warehouse wants one large zone for a single product, shaped like a rectangle.
Your team lead asks: "How many cells does the largest rectangle of one product cover?" A floor map can have up to 300 rows and 300 columns.
Write largest_block(floor).
floor is a list of equal-length strings, one for each row. A letter is the product in that cell.Input: floor = ["aab", "aab", "abb"]
Output: 4For each cell, count how many cells in its column, going up from it, hold the same letter, including the cell itself.
| Row | Letters | Heights |
|---|---|---|
| 0 | aab | [1, 1, 1] |
| 1 | aab | [2, 2, 2] |
| 2 | abb | [3, 1, 3] |
In row 1, the letters a, a have heights 2, 2: a rectangle of width 2 and height 2, which is 4 cells. Nothing larger exists.
Input: floor = ["xxxx"]
Output: 4One row of four x cells is a block of 4 cells.
Input: floor = ["ab", "ba"]
Output: 1No two neighbouring cells hold the same letter, so each block is a single cell.
1 ≤ len(floor) ≤ 300, and every row has the same length from 1 to 300a to zThe floor has 90,000 cells and 2,038,522,500 rectangles. Checking each rectangle cell by cell is far too slow. A method that visits each cell a small, fixed number of times has about 90,000 steps, which is trivial.