1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/矩阵的最大非负积 [maximum-non-negative-product-in-a-matrix].html

44 lines
2.0 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一个大小为 <code>m x n</code> 的矩阵 <code>grid</code> 。最初,你位于左上角 <code>(0, 0)</code> ,每一步,你可以在矩阵中 <strong>向右</strong><strong>向下</strong> 移动。</p>
<p>在从左上角 <code>(0, 0)</code> 开始到右下角 <code>(m - 1, n - 1)</code> 结束的所有路径中,找出具有 <strong>最大非负积</strong> 的路径。路径的积是沿路径访问的单元格中所有整数的乘积。</p>
<p>返回 <strong>最大非负积 </strong><strong><em> </em><code>10<sup>9</sup>&nbsp;+ 7</code></strong> <strong>取余</strong> 的结果。如果最大积为 <strong>负数</strong> ,则返回<em> </em><code>-1</code></p>
<p><strong>注意,</strong>取余是在得到最大积之后执行的。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/23/product1.jpg" style="width: 244px; height: 245px;" />
<pre>
<strong>输入:</strong>grid = [[-1,-2,-3],[-2,-3,-3],[-3,-3,-2]]
<strong>输出:</strong>-1
<strong>解释:</strong>从 (0, 0) 到 (2, 2) 的路径中无法得到非负积,所以返回 -1 。</pre>
<p><strong>示例 2</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/23/product2.jpg" style="width: 244px; height: 245px;" />
<pre>
<strong>输入:</strong>grid = [[1,-2,1],[1,-2,1],[3,-4,1]]
<strong>输出:</strong>8
<strong>解释:</strong>最大非负积对应的路径如图所示 (1 * 1 * -2 * -4 * 1 = 8)
</pre>
<p><strong>示例 3</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/23/product3.jpg" style="width: 164px; height: 165px;" />
<pre>
<strong>输入:</strong>grid = [[1,3],[0,-4]]
<strong>输出:</strong>0
<strong>解释:</strong>最大非负积对应的路径如图所示 (1 * 0 * -4 = 0)
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 &lt;= m, n &lt;= 15</code></li>
<li><code>-4 &lt;= grid[i][j] &lt;= 4</code></li>
</ul>