Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given a 2D matrix grid consisting of positive integers.
You have to select one or more cells from the matrix such that the following conditions are satisfied:
Your score will be the sum of the values of the selected cells.
Return the maximum score you can achieve.
Example 1:
Input: grid = [[1,2,3],[4,3,2],[1,1,1]]
Output: 8
Explanation:
We can select the cells with values 1, 3, and 4 that are colored above.
Example 2:
Input: grid = [[8,7,6],[8,3,2]]
Output: 15
Explanation:
We can select the cells with values 7 and 8 that are colored above.
Constraints:
1 <= grid.length, grid[i].length <= 101 <= grid[i][j] <= 100Problem summary: You are given a 2D matrix grid consisting of positive integers. You have to select one or more cells from the matrix such that the following conditions are satisfied: No two selected cells are in the same row of the matrix. The values in the set of selected cells are unique. Your score will be the sum of the values of the selected cells. Return the maximum score you can achieve.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming · Bit Manipulation
[[1,2,3],[4,3,2],[1,1,1]]
[[8,7,6],[8,3,2]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3276: Select Cells in Grid With Maximum Score
class Solution {
public int maxScore(List<List<Integer>> grid) {
int m = grid.size();
int mx = 0;
boolean[][] g = new boolean[101][m + 1];
for (int i = 0; i < m; ++i) {
for (int x : grid.get(i)) {
g[x][i] = true;
mx = Math.max(mx, x);
}
}
int[][] f = new int[mx + 1][1 << m];
for (int i = 1; i <= mx; ++i) {
for (int j = 0; j < 1 << m; ++j) {
f[i][j] = f[i - 1][j];
for (int k = 0; k < m; ++k) {
if (g[i][k] && (j >> k & 1) == 1) {
f[i][j] = Math.max(f[i][j], f[i - 1][j ^ 1 << k] + i);
}
}
}
}
return f[mx][(1 << m) - 1];
}
}
// Accepted solution for LeetCode #3276: Select Cells in Grid With Maximum Score
func maxScore(grid [][]int) int {
m := len(grid)
mx := 0
g := [101][11]bool{}
for i, row := range grid {
for _, x := range row {
g[x][i] = true
mx = max(mx, x)
}
}
f := make([][]int, mx+1)
for i := range f {
f[i] = make([]int, 1<<m)
}
for i := 1; i <= mx; i++ {
for j := 0; j < 1<<m; j++ {
f[i][j] = f[i-1][j]
for k := 0; k < m; k++ {
if g[i][k] && (j>>k&1) == 1 {
f[i][j] = max(f[i][j], f[i-1][j^1<<k]+i)
}
}
}
}
return f[mx][1<<m-1]
}
# Accepted solution for LeetCode #3276: Select Cells in Grid With Maximum Score
class Solution:
def maxScore(self, grid: List[List[int]]) -> int:
g = defaultdict(set)
mx = 0
for i, row in enumerate(grid):
for x in row:
g[x].add(i)
mx = max(mx, x)
m = len(grid)
f = [[0] * (1 << m) for _ in range(mx + 1)]
for i in range(1, mx + 1):
for j in range(1 << m):
f[i][j] = f[i - 1][j]
for k in g[i]:
if j >> k & 1:
f[i][j] = max(f[i][j], f[i - 1][j ^ 1 << k] + i)
return f[-1][-1]
// Accepted solution for LeetCode #3276: Select Cells in Grid With Maximum Score
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3276: Select Cells in Grid With Maximum Score
// class Solution {
// public int maxScore(List<List<Integer>> grid) {
// int m = grid.size();
// int mx = 0;
// boolean[][] g = new boolean[101][m + 1];
// for (int i = 0; i < m; ++i) {
// for (int x : grid.get(i)) {
// g[x][i] = true;
// mx = Math.max(mx, x);
// }
// }
// int[][] f = new int[mx + 1][1 << m];
// for (int i = 1; i <= mx; ++i) {
// for (int j = 0; j < 1 << m; ++j) {
// f[i][j] = f[i - 1][j];
// for (int k = 0; k < m; ++k) {
// if (g[i][k] && (j >> k & 1) == 1) {
// f[i][j] = Math.max(f[i][j], f[i - 1][j ^ 1 << k] + i);
// }
// }
// }
// }
// return f[mx][(1 << m) - 1];
// }
// }
// Accepted solution for LeetCode #3276: Select Cells in Grid With Maximum Score
function maxScore(grid: number[][]): number {
const m = grid.length;
let mx = 0;
const g: boolean[][] = Array.from({ length: 101 }, () => Array(m + 1).fill(false));
for (let i = 0; i < m; ++i) {
for (const x of grid[i]) {
g[x][i] = true;
mx = Math.max(mx, x);
}
}
const f: number[][] = Array.from({ length: mx + 1 }, () => Array(1 << m).fill(0));
for (let i = 1; i <= mx; ++i) {
for (let j = 0; j < 1 << m; ++j) {
f[i][j] = f[i - 1][j];
for (let k = 0; k < m; ++k) {
if (g[i][k] && ((j >> k) & 1) === 1) {
f[i][j] = Math.max(f[i][j], f[i - 1][j ^ (1 << k)] + i);
}
}
}
}
return f[mx][(1 << m) - 1];
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.