Maximum Score From Grid Operations Problem
Description
LeetCode Problem 3225.
You are given a 2D matrix grid of size n x n. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices (i, j), and color black all the cells of the j^th column starting from the top row down to the i^th row.
The grid score is the sum of all grid[i][j] such that cell (i, j) is white and it has a horizontally adjacent black cell.
Return the maximum score that can be achieved after some number of operations.
Example 1:
Input: grid = [[0,0,0,0,0],[0,0,3,0,0],[0,1,0,0,0],[5,0,0,3,0],[0,0,0,0,2]]
Output: 11
Explanation:

In the first operation, we color all cells in column 1 down to row 3, and in the second operation, we color all cells in column 4 down to the last row. The score of the resulting grid is grid[3][0] + grid[1][2] + grid[3][3] which is equal to 11.
Example 2:
Input: grid = [[10,9,0,0,15],[7,1,0,8,0],[5,20,0,11,0],[0,0,0,1,2],[8,12,1,10,3]]
Output: 94
Explanation:

We perform operations on 1, 2, and 3 down to rows 1, 4, and 0, respectively. The score of the resulting grid is grid[0][0] + grid[1][0] + grid[2][1] + grid[4][1] + grid[1][3] + grid[2][3] + grid[3][3] + grid[4][3] + grid[0][4] which is equal to 94.
Constraints:
1 <= n == grid.length <= 100n == grid[i].length0 <= grid[i][j] <= 10^9
Solution
Approach: Dynamic Programming + Prefix Sums
For each column j let k[j] in 0..n be the number of cells painted black from the top. A white cell scores if at least one horizontal neighbor is black, and each cell is counted once. So column j contributes
max(0, s[j][max(k[j-1], k[j+1])] - s[j][k[j]])
where s[j][h] is the sum of the first h cells of column j (columns outside the grid have height 0).
Let f[h1][h2] be the best score after processing a column of height h1 whose left neighbor has height h2. For the next height hp, the transition adds max(0, s[j][max(h2, hp)] - s[j][h1]). Split on h2 <= hp and h2 > hp and keep prefix and suffix maxima over h2, so each column costs O(n^2) instead of O(n^3).
Complexity: time O(n^3), space O(n^2), where n is the grid size.
from math import inf
class Solution:
def maximumScore(self, grid: List[List[int]]) -> int:
n = len(grid)
s = [[0] * (n + 1) for _ in range(n)]
for j in range(n):
for i, x in enumerate(grid):
s[j][i + 1] = s[j][i] + x[j]
f = [[-inf] * (n + 1) for _ in range(n + 1)]
for h in range(n + 1):
f[h][0] = 0
for j in range(n - 1):
g = [[-inf] * (n + 1) for _ in range(n + 1)]
for h1 in range(n + 1):
pre = [-inf] * (n + 2)
pre[0] = f[h1][0]
for h2 in range(1, n + 1):
pre[h2] = max(pre[h2 - 1], f[h1][h2])
suf = [-inf] * (n + 2)
for h2 in range(n, -1, -1):
v = -inf
if f[h1][h2] != -inf:
v = f[h1][h2] + max(0, s[j][h2] - s[j][h1])
suf[h2] = max(suf[h2 + 1], v)
for hp in range(n + 1):
add = max(0, s[j][hp] - s[j][h1])
v1 = -inf if pre[hp] == -inf else pre[hp] + add
g[hp][h1] = max(v1, suf[hp + 1])
f = g
ans = 0
for h1 in range(n + 1):
for h2 in range(n + 1):
if f[h1][h2] != -inf:
ans = max(ans, f[h1][h2] + max(0, s[-1][h2] - s[-1][h1]))
return ans