field[i][j] is the profit of a cell (it can be negative when a cell costs money to work). Choose one plot, a rectangle of cells aligned with the grid, whose total profit does not exceed cap.
Return the largest total profit such a plot can have. At least one valid plot is guaranteed to exist.
Follow-up: how would you change the approach if there are far more rows than columns?field=[[3, -1], [2, 4]], cap=5 → 5
The left column gives 3 + 2 = 5, exactly the cap. Rectangles with larger sums (such as the bottom row, 6) are not allowed.
field=[[-2, 7, 1]], cap=0 → -2
The only rectangle whose sum is at most 0 is the single cell -2.
capfield=[[3, -1], [2, 4]], cap=5
5
Run checks the first 3 tests in your browser. Submit checks all 200 tests on the server, hidden ones included.