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 Normal View History

2023-12-09 18:42:21 +08:00
<p>给你一个大小为 <code>m x n</code> 的矩阵 <code>grid</code> 。最初,你位于左上角 <code>(0, 0)</code> ,每一步,你可以在矩阵中 <strong>向右</strong><strong>向下</strong> 移动。</p>
2022-03-27 20:45:09 +08:00
2023-12-09 18:42:21 +08:00
<p>在从左上角 <code>(0, 0)</code> 开始到右下角 <code>(m - 1, n - 1)</code> 结束的所有路径中,找出具有 <strong>最大非负积</strong> 的路径。路径的积是沿路径访问的单元格中所有整数的乘积。</p>
2022-03-27 20:45:09 +08:00
2023-12-09 18:42:21 +08:00
<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>
2022-03-27 20:45:09 +08:00
<p><strong>注意,</strong>取余是在得到最大积之后执行的。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
2023-12-09 18:42:21 +08:00
<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]]
2022-03-27 20:45:09 +08:00
<strong>输出:</strong>-1
2023-12-09 18:42:21 +08:00
<strong>解释:</strong>从 (0, 0) 到 (2, 2) 的路径中无法得到非负积,所以返回 -1 。</pre>
2022-03-27 20:45:09 +08:00
<p><strong>示例 2</strong></p>
2023-12-09 18:42:21 +08:00
<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]]
2022-03-27 20:45:09 +08:00
<strong>输出:</strong>8
2023-12-09 18:42:21 +08:00
<strong>解释:</strong>最大非负积对应的路径如图所示 (1 * 1 * -2 * -4 * 1 = 8)
2022-03-27 20:45:09 +08:00
</pre>
<p><strong>示例 3</strong></p>
2023-12-09 18:42:21 +08:00
<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]]
2022-03-27 20:45:09 +08:00
<strong>输出:</strong>0
2023-12-09 18:42:21 +08:00
<strong>解释:</strong>最大非负积对应的路径如图所示 (1 * 0 * -4 = 0)
2022-03-27 20:45:09 +08:00
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
2023-12-09 18:42:21 +08:00
<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>
2022-03-27 20:45:09 +08:00
<li><code>-4 &lt;= grid[i][j] &lt;= 4</code></li>
</ul>