3225. Maximum Score From Grid Operations
Difficulty: Hard
Topics: Principal, Array, Dynamic Programming, Matrix, Prefix Sum, Biweekly Contest 135
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ᵗʰ column starting from the top row down to the iᵗʰ 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⁹
Hint:
- Use dynamic programming.
- Solve the problem in
O(N⁴)using a 3-states dp. - Let
dp[i][lastHeight][beforeLastHeight]denote the maximum score if the grid was limited to columni, and the height of columni - 1islastHeightand the height of columni - 2isbeforeLastHeight. - The third state,
beforeLastHeight, is used to determine which values of columni - 1will be added to the score. We can replace this state with another state that only takes two values 0 or 1. - Let
dp[i][lastHeight][isBigger]denote the maximum score if the grid was limited to columni, and where the height of columni - 1islastHeight. Additionally, ifisBigger == 1, the number of black cells in columniis assumed to be larger than the number of black cells in columni - 2, and vice versa. Note that if our assumption is wrong, it would lead to a suboptimal score and, therefore, it would not be considered as the final answer.
Solution:
The problem involves coloring cells in columns from top to a chosen row, and the score is the sum of white cells that have a black horizontally adjacent cell.
The solution uses Dynamic Programming with two states per column, tracking whether the current column’s black height is greater than the previous one or not.
It runs in O(n³) and handles up to n = 100 efficiently.
Approach:
- Prefix sums per column are precomputed to quickly get sums of ranges.
-
DP states:
-
dpPick[h]: max score if current column's black height is exactlyhand the previous column's black height is less (so we count some cells in the previous column). -
dpSkip[h]: same but the previous column's black height is greater (so we don’t count those cells in the previous column).
-
- Transition between columns considers two cases:
- Current height > previous height: add cells from previous column between
prevandcurr. - Current height ≤ previous height: add cells from current column between
currandprev.
- Current height > previous height: add cells from previous column between
- The “skip” state is used to handle the case where no horizontal adjacency is possible from the previous column.
Let's implement this solution in PHP: 3225. Maximum Score From Grid Operations
<?php
/**
* @param Integer[][] $grid
* @return Integer
*/
function maximumScore(array $grid): int
{
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo maximumScore([[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]]) . "\n"; // Output: 11
echo maximumScore([[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]]) . "\n"; // Output: 94
?>
Explanation:
-
Step 1 — Prefix sums:
-
prefix[j][i]= sum of firsticells in columnj(0-based rows). -
prefix[j][curr] - prefix[j][prev]= sum of rows fromprevtocurr-1in columnj.
-
-
Step 2 — DP initialization: For the first column,
dpPickanddpSkipare just zero because no previous column exists yet. -
Step 3 — Transition for column
j:- For each possible current black height
currand previous heightprev: - If
curr > prev:- We must add the segment
[prev, curr)from previous column (because cells in previous column fromprevtocurr-1are white and have black in current column). - Update
dpPick[curr]anddpSkip[curr]from previousdpSkip[prev](since prev's height was ≤ prev's prev height, so we use dpSkip).
- We must add the segment
- If
curr ≤ prev:- We must add the segment
[curr, prev)from current column (because cells in current column fromcurrtoprev-1are white and have black in previous column). - Update
dpPick[curr]from previousdpPick[prev]. - Update
dpSkip[curr]from previousdpPick[prev](since prev's height > prev's prev height, so we use dpPick but without adding score here — wait, this is subtle but correct because indpSkipwe don't count the current column’s segment).
- We must add the segment
- For each possible current black height
-
Step 4 — Result: After processing all columns, the answer is
max(dpPick)because the last column can end with any height and its pick state is valid.
Complexity
-
Time Complexity: O(n³), We have
O(n)columns,O(n²)height pairs, andO(n)for inner loops — actually careful: For eachj, we loopcurr(0..n)andprev(0..n) → O(n²). Inside we doO(1)work. So totalO(n³). -
Space Complexity: O(n), We only store two arrays of size
n+1fordpPickanddpSkipper column.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
Top comments (0)