java.lang.Object
g1501_1600.s1594_maximum_non_negative_product_in_a_matrix.Solution

public class Solution extends Object
1594 - Maximum Non Negative Product in a Matrix.<p>Medium</p> <p>You are given a <code>m x n</code> matrix <code>grid</code>. Initially, you are located at the top-left corner <code>(0, 0)</code>, and in each step, you can only <strong>move right or down</strong> in the matrix.</p> <p>Among all possible paths starting from the top-left corner <code>(0, 0)</code> and ending in the bottom-right corner <code>(m - 1, n - 1)</code>, find the path with the <strong>maximum non-negative product</strong>. The product of a path is the product of all integers in the grid cells visited along the path.</p> <p>Return the <em>maximum non-negative product <strong>modulo</strong></em> <code>10<sup>9</sup> + 7</code>. <em>If the maximum product is <strong>negative</strong> , return</em> <code>-1</code>.</p> <p>Notice that the modulo is performed after getting the maximum product.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/12/23/product1.jpg" alt="" /></p> <p><strong>Input:</strong> grid = [[-1,-2,-3],[-2,-3,-3],[-3,-3,-2]]</p> <p><strong>Output:</strong> -1</p> <p><strong>Explanation:</strong> It is not possible to get non-negative product in the path from (0, 0) to (2, 2), so return -1.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/12/23/product2.jpg" alt="" /></p> <p><strong>Input:</strong> grid = [[1,-2,1],[1,-2,1],[3,-4,1]]</p> <p><strong>Output:</strong> 8</p> <p><strong>Explanation:</strong> Maximum non-negative product is shown (1 * 1 * -2 * -4 * 1 = 8).</p> <p><strong>Example 3:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/12/23/product3.jpg" alt="" /></p> <p><strong>Input:</strong> grid = [[1,3],[0,-4]]</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> Maximum non-negative product is shown (1 * 0 * -4 = 0).</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 <= m, n <= 15</code></li> <li><code>-4 <= grid[i][j] <= 4</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maxProductPath

      public int maxProductPath(int[][] grid)