mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
46 lines
1.8 KiB
HTML
46 lines
1.8 KiB
HTML
|
<p>给你一个 <code>n x n</code> 的二进制网格 <code>grid</code>,每一次操作中,你可以选择网格的 <strong>相邻两行</strong> 进行交换。</p>
|
|||
|
|
|||
|
<p>一个符合要求的网格需要满足主对角线以上的格子全部都是 <strong>0</strong> 。</p>
|
|||
|
|
|||
|
<p>请你返回使网格满足要求的最少操作次数,如果无法使网格符合要求,请你返回 <strong>-1</strong> 。</p>
|
|||
|
|
|||
|
<p>主对角线指的是从 <code>(1, 1)</code> 到 <code>(n, n)</code> 的这些格子。</p>
|
|||
|
|
|||
|
<p> </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> </p>
|
|||
|
|
|||
|
<p><strong>提示:</strong></p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li><code>n == grid.length</code></li>
|
|||
|
<li><code>n == grid[i].length</code></li>
|
|||
|
<li><code>1 <= n <= 200</code></li>
|
|||
|
<li><code>grid[i][j]</code> 要么是 <code>0</code> 要么是 <code>1</code> 。</li>
|
|||
|
</ul>
|