Problem 248641 · medium · Level 02 Linear Data Structures

Best Press of the Egg-Timer Stamp

2d-lists · shapes · nested-loops

A rubber stamp shaped like an egg timer (an hourglass) covers seven cells of a 3 x 3 square: the whole top row, the centre cell and the whole bottom row (# is covered, . is not):

# # #
. # .
# # #

The stamp may be pressed anywhere on the integer grid grid (at least 3 rows and 3 columns) as long as it lies completely inside, without turning it. A press scores the sum of the seven cells it covers. Return the best score any press can get.

Examples

Input:  grid = [[2, 0, 3, 1],
                [1, 4, 2, 0],
                [0, 1, 5, 2],
                [3, 0, 1, 1]]
Output: 15
Explanation: the press in the top-left corner covers 2+0+3 + 4 + 0+1+5.

Input:  grid = [[-1, -1, -1],
                [-1, -9, -1],
                [-1, -1, -1]]
Output: -15

Constraints

  • 3 <= rows, cols <= 100, -100 <= grid[r][c] <= 100

Goals

  • Sum a fixed non-rectangular shape at every valid position
  • Track a maximum that may be negative
Starting Python…