Skip to content

Practice Maximum Non Negative Product in a Matrix

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1594
LeetCode Problem

Maximum Non Negative Product in a Matrix

Problem Statement

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 10^9 + 7. If the maximum product is negative, return -1. Notice that the modulo is performed after getting the maximum product.

Source: Maximum Non Negative Product in a Matrix on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
grid = [[-1,-2,-3],[-2,-3,-3],[-3,-3,-2]]
Output
-1

# Example 2

Input
grid = [[1,-2,1],[1,-2,1],[3,-4,1]]
Output
8

# Example 3

Input
grid = [[1,3],[0,-4]]
Output
0

Constraints

m == grid.length
n == grid[i].length
1 <= m, n <= 15
-4 <= grid[i][j] <= 4

Related LeetCode Problems

Practice more problems similar to Maximum Non Negative Product in a Matrix.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.