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 a m x n matrix grid. Initially, you are located at the top-left corner (0, 0), and in each step, you can only move right or down in the matrix.
Among all possible paths starting from the top-left corner (0, 0) and ending in the bottom-right corner (m - 1, n - 1), find the path with the maximum non-negative product. The product of a path is the product of all integers in the grid cells visited along the path.
Return the maximum non-negative product modulo 109 + 7. If the maximum product is negative, return -1.
Notice that the modulo is performed after getting the maximum product.
Example 1:
Input: grid = [[-1,-2,-3],[-2,-3,-3],[-3,-3,-2]] Output: -1 Explanation: It is not possible to get non-negative product in the path from (0, 0) to (2, 2), so return -1.
Example 2:
Input: grid = [[1,-2,1],[1,-2,1],[3,-4,1]] Output: 8 Explanation: Maximum non-negative product is shown (1 * 1 * -2 * -4 * 1 = 8).
Example 3:
Input: grid = [[1,3],[0,-4]] Output: 0 Explanation: Maximum non-negative product is shown (1 * 0 * -4 = 0).
Constraints:
m == grid.lengthn == grid[i].length1 <= m, n <= 15-4 <= grid[i][j] <= 4Problem summary: You are given a m x n matrix grid. Initially, you are located at the top-left corner (0, 0), and in each step, you can only move right or down in the matrix. Among all possible paths starting from the top-left corner (0, 0) and ending in the bottom-right corner (m - 1, n - 1), find the path with the maximum non-negative product. The product of a path is the product of all integers in the grid cells visited along the path. Return the maximum non-negative product modulo 109 + 7. If the maximum product is negative, return -1. Notice that the modulo is performed after getting the maximum product.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[[-1,-2,-3],[-2,-3,-3],[-3,-3,-2]]
[[1,-2,1],[1,-2,1],[3,-4,1]]
[[1,3],[0,-4]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1594: Maximum Non Negative Product in a Matrix
class Solution {
private static final int MOD = (int) 1e9 + 7;
public int maxProductPath(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
long[][][] dp = new long[m][n][2];
dp[0][0][0] = grid[0][0];
dp[0][0][1] = grid[0][0];
for (int i = 1; i < m; ++i) {
dp[i][0][0] = dp[i - 1][0][0] * grid[i][0];
dp[i][0][1] = dp[i - 1][0][1] * grid[i][0];
}
for (int j = 1; j < n; ++j) {
dp[0][j][0] = dp[0][j - 1][0] * grid[0][j];
dp[0][j][1] = dp[0][j - 1][1] * grid[0][j];
}
for (int i = 1; i < m; ++i) {
for (int j = 1; j < n; ++j) {
int v = grid[i][j];
if (v >= 0) {
dp[i][j][0] = Math.min(dp[i - 1][j][0], dp[i][j - 1][0]) * v;
dp[i][j][1] = Math.max(dp[i - 1][j][1], dp[i][j - 1][1]) * v;
} else {
dp[i][j][0] = Math.max(dp[i - 1][j][1], dp[i][j - 1][1]) * v;
dp[i][j][1] = Math.min(dp[i - 1][j][0], dp[i][j - 1][0]) * v;
}
}
}
long ans = dp[m - 1][n - 1][1];
return ans < 0 ? -1 : (int) (ans % MOD);
}
}
// Accepted solution for LeetCode #1594: Maximum Non Negative Product in a Matrix
func maxProductPath(grid [][]int) int {
m, n := len(grid), len(grid[0])
dp := make([][][]int, m)
for i := range dp {
dp[i] = make([][]int, n)
for j := range dp[i] {
dp[i][j] = make([]int, 2)
}
}
dp[0][0] = []int{grid[0][0], grid[0][0]}
for i := 1; i < m; i++ {
dp[i][0][0] = dp[i-1][0][0] * grid[i][0]
dp[i][0][1] = dp[i-1][0][1] * grid[i][0]
}
for j := 1; j < n; j++ {
dp[0][j][0] = dp[0][j-1][0] * grid[0][j]
dp[0][j][1] = dp[0][j-1][1] * grid[0][j]
}
for i := 1; i < m; i++ {
for j := 1; j < n; j++ {
v := grid[i][j]
if v >= 0 {
dp[i][j][0] = min(dp[i-1][j][0], dp[i][j-1][0]) * v
dp[i][j][1] = max(dp[i-1][j][1], dp[i][j-1][1]) * v
} else {
dp[i][j][0] = max(dp[i-1][j][1], dp[i][j-1][1]) * v
dp[i][j][1] = min(dp[i-1][j][0], dp[i][j-1][0]) * v
}
}
}
ans := dp[m-1][n-1][1]
if ans < 0 {
return -1
}
var mod int = 1e9 + 7
return ans % mod
}
# Accepted solution for LeetCode #1594: Maximum Non Negative Product in a Matrix
class Solution:
def maxProductPath(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
mod = 10**9 + 7
dp = [[[grid[0][0]] * 2 for _ in range(n)] for _ in range(m)]
for i in range(1, m):
dp[i][0] = [dp[i - 1][0][0] * grid[i][0]] * 2
for j in range(1, n):
dp[0][j] = [dp[0][j - 1][0] * grid[0][j]] * 2
for i in range(1, m):
for j in range(1, n):
v = grid[i][j]
if v >= 0:
dp[i][j][0] = min(dp[i - 1][j][0], dp[i][j - 1][0]) * v
dp[i][j][1] = max(dp[i - 1][j][1], dp[i][j - 1][1]) * v
else:
dp[i][j][0] = max(dp[i - 1][j][1], dp[i][j - 1][1]) * v
dp[i][j][1] = min(dp[i - 1][j][0], dp[i][j - 1][0]) * v
ans = dp[-1][-1][1]
return -1 if ans < 0 else ans % mod
// Accepted solution for LeetCode #1594: Maximum Non Negative Product in a Matrix
struct Solution;
const MOD: i64 = 1_000_000_007;
impl Solution {
fn max_product_path(grid: Vec<Vec<i32>>) -> i32 {
let n = grid.len();
let m = grid[0].len();
let mut max = vec![vec![0; m]; n];
let mut min = vec![vec![0; m]; n];
max[0][0] = grid[0][0] as i64;
min[0][0] = grid[0][0] as i64;
for i in 1..n {
max[i][0] = max[i - 1][0] * grid[i][0] as i64;
min[i][0] = min[i - 1][0] * grid[i][0] as i64;
}
for j in 1..m {
max[0][j] = max[0][j - 1] * grid[0][j] as i64;
min[0][j] = min[0][j - 1] * grid[0][j] as i64;
}
for i in 1..n {
for j in 1..m {
if grid[i][j] < 0 {
max[i][j] = grid[i][j] as i64 * min[i][j - 1].min(min[i - 1][j]);
min[i][j] = grid[i][j] as i64 * max[i][j - 1].max(max[i - 1][j]);
} else {
max[i][j] = grid[i][j] as i64 * max[i][j - 1].max(max[i - 1][j]);
min[i][j] = grid[i][j] as i64 * min[i][j - 1].min(min[i - 1][j]);
}
}
}
if max[n - 1][m - 1] < 0 {
-1
} else {
(max[n - 1][m - 1] % MOD) as i32
}
}
}
#[test]
fn test() {
let grid = vec_vec_i32![[-1, -2, -3], [-2, -3, -3], [-3, -3, -2]];
let res = -1;
assert_eq!(Solution::max_product_path(grid), res);
let grid = vec_vec_i32![[1, -2, 1], [1, -2, 1], [3, -4, 1]];
let res = 8;
assert_eq!(Solution::max_product_path(grid), res);
let grid = vec_vec_i32![[1, 3], [0, -4]];
let res = 0;
assert_eq!(Solution::max_product_path(grid), res);
let grid = vec_vec_i32![[1, 4, 4, 0], [-2, 0, 0, 1], [1, -1, 1, 1]];
let res = 2;
assert_eq!(Solution::max_product_path(grid), res);
}
// Accepted solution for LeetCode #1594: Maximum Non Negative Product in a Matrix
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1594: Maximum Non Negative Product in a Matrix
// class Solution {
// private static final int MOD = (int) 1e9 + 7;
//
// public int maxProductPath(int[][] grid) {
// int m = grid.length;
// int n = grid[0].length;
// long[][][] dp = new long[m][n][2];
// dp[0][0][0] = grid[0][0];
// dp[0][0][1] = grid[0][0];
// for (int i = 1; i < m; ++i) {
// dp[i][0][0] = dp[i - 1][0][0] * grid[i][0];
// dp[i][0][1] = dp[i - 1][0][1] * grid[i][0];
// }
// for (int j = 1; j < n; ++j) {
// dp[0][j][0] = dp[0][j - 1][0] * grid[0][j];
// dp[0][j][1] = dp[0][j - 1][1] * grid[0][j];
// }
// for (int i = 1; i < m; ++i) {
// for (int j = 1; j < n; ++j) {
// int v = grid[i][j];
// if (v >= 0) {
// dp[i][j][0] = Math.min(dp[i - 1][j][0], dp[i][j - 1][0]) * v;
// dp[i][j][1] = Math.max(dp[i - 1][j][1], dp[i][j - 1][1]) * v;
// } else {
// dp[i][j][0] = Math.max(dp[i - 1][j][1], dp[i][j - 1][1]) * v;
// dp[i][j][1] = Math.min(dp[i - 1][j][0], dp[i][j - 1][0]) * v;
// }
// }
// }
// long ans = dp[m - 1][n - 1][1];
// return ans < 0 ? -1 : (int) (ans % MOD);
// }
// }
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.