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 jth column starting from the top row down to the ith 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.
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: 11Explanation:

In the first operation, we color all cells in column 1 down to row 3, and inthe 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 isequal to 11.
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: 94Explanation:

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.
For each column, we can choose to color any prefix (from the top down to some row). The goal is to maximize the sum of all white cells that have a horizontally adjacent black cell. The optimal strategy is to, for each column, try all possible cut-off rows and use prefix sums to efficiently compute the score for each configuration.
classSolution {
publicintmaximumScore(int[][] grid) {
int n = grid.length, ans = 0;
int[] cut =newint[n];
Arrays.fill(cut, -1);
ans = dfs(grid, cut, 0, n, 0);
return ans;
}
privateintdfs(int[][] grid, int[] cut, int col, int n, int ans) {
if (col == n) {
int score = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i > cut[j]) {
if ((j > 0 && i <= cut[j-1]) || (j < n-1 && i <= cut[j+1]))
score += grid[i][j];
}
}
}
return Math.max(ans, score);
}
int res = ans;
for (int r =-1; r < n; ++r) {
cut[col]= r;
res = Math.max(res, dfs(grid, cut, col+1, n, ans));
}
return res;
}
}
classSolution {
funmaximumScore(grid: Array<IntArray>): Int {
val n = grid.size
var ans = 0val cut = IntArray(n) { -1 }
fundfs(col: Int) {
if (col == n) {
var score = 0for (i in0 until n) {
for (j in0 until n) {
if (i > cut[j]) {
if ((j > 0&& i <= cut[j-1]) || (j < n-1&& i <= cut[j+1]))
score += grid[i][j]
}
}
}
ans = maxOf(ans, score)
return }
for (r in -1 until n) {
cut[col] = r
dfs(col+1)
}
}
dfs(0)
return ans
}
}
classSolution:
defmaximumScore(self, grid: list[list[int]]) -> int:
n = len(grid)
ans =0 cut = [-1] * n
defdfs(col):
nonlocal ans
if col == n:
score =0for i in range(n):
for j in range(n):
if i > cut[j]:
if (j >0and i <= cut[j-1]) or (j < n-1and i <= cut[j+1]):
score += grid[i][j]
ans = max(ans, score)
returnfor r in range(-1, n):
cut[col] = r
dfs(col+1)
dfs(0)
return ans
⏰ Time complexity: O(n^n * n^2), as we try all possible cut-off rows for each column (not feasible for large n, but optimal for small n or for interview discussion).
🧺 Space complexity: O(n), for the recursion stack and cut array.