#1594
Maximum Non Negative Product in a Matrix
expert · 1060 · lc medium +32 · verified · 35.8% accepted · 941 likes · top 14%
Description
You are given an m x n matrix grid. Starting at (0, 0) and moving only right or down, find the path to (m-1, n-1) that yields the maximum non-negative product of all cell values along the path. Return this product modulo 109 + 7. If every possible path has a negative product, return -1. Note: the modulo is applied only after determining 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).
Code
1
2
3