FunctionRacerProblemsRace

Richest Square Block

EasyGridsPython, JavaScript, TypeScript, Java, C++

Among all square blocks of k rows and k columns that fit completely inside grid (a list of rows), find the block whose cells add up to the largest total, and return the position [row, col] of its top-left cell. If several blocks share the largest total, return the one whose top-left cell comes first in reading order: the smallest row, and among those the smallest column. k is never larger than the number of rows or the number of columns. Do not change grid.

Example 1

grid [[1,2,0],[3,4,1],[0,1,9]]
k 2
returns [1,1]

The four 2 by 2 blocks add up to 10, 7, 8 and 15; the block starting at row 1, column 1 has the largest total.

Example 2

grid [[4,0,4],[0,0,0]]
k 1
returns [0,0]

With k = 1 every block is a single cell. The two 4s tie, and the one in column 0 comes first in reading order.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Grids problems