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.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an m x n matrix grid consisting of positive integers. You can move from a cell in the matrix to any other cell that is either to the bottom or to the right (not necessarily adjacent). The score of a move from a cell with the value c1 to a cell with the value c2 is c2 - c1.
You can start at any cell, and you have to make at least one move.
Return the maximum total score you can achieve.
Example 1:
Input: grid = [[9,5,7,3],[8,9,6,1],[6,7,14,3],[2,5,3,1]]
Output: 9
Explanation: We start at the cell (0, 1), and we perform the following moves:
- Move from the cell (0, 1) to (2, 1) with a score of 7 - 5 = 2.
- Move from the cell (2, 1) to (2, 2) with a score of 14 - 7 = 7.
The total score is 2 + 7 = 9.
Example 2:
Input: grid = [[4,3,2],[3,2,1]]
Output: -1
Explanation: We start at the cell (0, 0), and we perform one move: (0, 0) to (0, 1). The score is 3 - 4 = -1.
Constraints:
m == grid.lengthn == grid[i].length2 <= m, n <= 10004 <= m * n <= 1051 <= grid[i][j] <= 105Problem summary: You are given an m x n matrix grid consisting of positive integers. You can move from a cell in the matrix to any other cell that is either to the bottom or to the right (not necessarily adjacent). The score of a move from a cell with the value c1 to a cell with the value c2 is c2 - c1. You can start at any cell, and you have to make at least one move. Return the maximum total score you can achieve.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[[9,5,7,3],[8,9,6,1],[6,7,14,3],[2,5,3,1]]
[[4,3,2],[3,2,1]]
maximum-score-from-grid-operations)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3148: Maximum Difference Score in a Grid
class Solution {
public int maxScore(List<List<Integer>> grid) {
int m = grid.size(), n = grid.get(0).size();
final int inf = 1 << 30;
int ans = -inf;
int[][] f = new int[m][n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int mi = inf;
if (i > 0) {
mi = Math.min(mi, f[i - 1][j]);
}
if (j > 0) {
mi = Math.min(mi, f[i][j - 1]);
}
ans = Math.max(ans, grid.get(i).get(j) - mi);
f[i][j] = Math.min(grid.get(i).get(j), mi);
}
}
return ans;
}
}
// Accepted solution for LeetCode #3148: Maximum Difference Score in a Grid
func maxScore(grid [][]int) int {
m, n := len(grid), len(grid[0])
f := make([][]int, m)
for i := range f {
f[i] = make([]int, n)
}
const inf int = 1 << 30
ans := -inf
for i, row := range grid {
for j, x := range row {
mi := inf
if i > 0 {
mi = min(mi, f[i-1][j])
}
if j > 0 {
mi = min(mi, f[i][j-1])
}
ans = max(ans, x-mi)
f[i][j] = min(x, mi)
}
}
return ans
}
# Accepted solution for LeetCode #3148: Maximum Difference Score in a Grid
class Solution:
def maxScore(self, grid: List[List[int]]) -> int:
f = [[0] * len(grid[0]) for _ in range(len(grid))]
ans = -inf
for i, row in enumerate(grid):
for j, x in enumerate(row):
mi = inf
if i:
mi = min(mi, f[i - 1][j])
if j:
mi = min(mi, f[i][j - 1])
ans = max(ans, x - mi)
f[i][j] = min(x, mi)
return ans
// Accepted solution for LeetCode #3148: Maximum Difference Score in a Grid
// 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 #3148: Maximum Difference Score in a Grid
// class Solution {
// public int maxScore(List<List<Integer>> grid) {
// int m = grid.size(), n = grid.get(0).size();
// final int inf = 1 << 30;
// int ans = -inf;
// int[][] f = new int[m][n];
// for (int i = 0; i < m; ++i) {
// for (int j = 0; j < n; ++j) {
// int mi = inf;
// if (i > 0) {
// mi = Math.min(mi, f[i - 1][j]);
// }
// if (j > 0) {
// mi = Math.min(mi, f[i][j - 1]);
// }
// ans = Math.max(ans, grid.get(i).get(j) - mi);
// f[i][j] = Math.min(grid.get(i).get(j), mi);
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3148: Maximum Difference Score in a Grid
function maxScore(grid: number[][]): number {
const [m, n] = [grid.length, grid[0].length];
const f: number[][] = Array.from({ length: m }, () => Array.from({ length: n }, () => 0));
let ans = -Infinity;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
let mi = Infinity;
if (i) {
mi = Math.min(mi, f[i - 1][j]);
}
if (j) {
mi = Math.min(mi, f[i][j - 1]);
}
ans = Math.max(ans, grid[i][j] - mi);
f[i][j] = Math.min(mi, grid[i][j]);
}
}
return ans;
}
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.