Richest Square Block
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
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
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
1 <= rows, cols <= 101 <= k <= min(rows, cols)-100 <= grid[r][c] <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Grids problems
- Ring WalkEasy
- Row and Column SwitchesEasy
- Same Letter RegionEasy
- Stones SettleEasy
- Turns to MatchEasy
- Turtle CommandsEasy
- Up and to the RightEasy
- Wireworld StepEasy