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)/排布二进制网格的最少交换次数 [minimum-swaps-to-arrange-a-binary-grid].html

46 lines
1.8 KiB
HTML
Raw Normal View History

2022-03-27 20:37:52 +08:00
<p>给你一个&nbsp;<code>n&nbsp;x n</code>&nbsp;的二进制网格&nbsp;<code>grid</code>,每一次操作中,你可以选择网格的&nbsp;<strong>相邻两行</strong>&nbsp;进行交换。</p>
<p>一个符合要求的网格需要满足主对角线以上的格子全部都是 <strong>0</strong>&nbsp;</p>
<p>请你返回使网格满足要求的最少操作次数,如果无法使网格符合要求,请你返回 <strong>-1</strong>&nbsp;</p>
<p>主对角线指的是从&nbsp;<code>(1, 1)</code>&nbsp;&nbsp;<code>(n, n)</code>&nbsp;的这些格子。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/08/02/fw.jpg" style="height: 141px; width: 750px;"></p>
<pre><strong>输入:</strong>grid = [[0,0,1],[1,1,0],[1,0,0]]
<strong>输出:</strong>3
</pre>
<p><strong>示例 2</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/08/02/e2.jpg" style="height: 270px; width: 270px;"></p>
<pre><strong>输入:</strong>grid = [[0,1,1,0],[0,1,1,0],[0,1,1,0],[0,1,1,0]]
<strong>输出:</strong>-1
<strong>解释:</strong>所有行都是一样的,交换相邻行无法使网格符合要求。
</pre>
<p><strong>示例 3</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/08/02/e3.jpg" style="height: 210px; width: 210px;"></p>
<pre><strong>输入:</strong>grid = [[1,0,0],[1,1,0],[1,1,1]]
<strong>输出:</strong>0
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 &lt;= n&nbsp;&lt;= 200</code></li>
<li><code>grid[i][j]</code>&nbsp;要么是&nbsp;<code>0</code>&nbsp;要么是&nbsp;<code>1</code>&nbsp;</li>
</ul>