1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-09 17:31:41 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
2022-05-02 23:44:12 +08:00
parent 7ea03594b3
commit 2a71c78585
4790 changed files with 11696 additions and 10944 deletions

View File

@@ -12,7 +12,7 @@
"translatedContent": "<p><strong>交换</strong> 定义为选中一个数组中的两个 <strong>互不相同</strong> 的位置并交换二者的值。</p>\n\n<p><strong>环形</strong> 数组是一个数组,可以认为 <strong>第一个</strong> 元素和 <strong>最后一个</strong> 元素 <strong>相邻</strong> 。</p>\n\n<p>给你一个 <strong>二进制环形</strong> 数组 <code>nums</code> ,返回在 <strong>任意位置</strong> 将数组中的所有 <code>1</code> 聚集在一起需要的最少交换次数。</p>\n\n<p>&nbsp;</p>\n\n<p><strong>示例 1</strong></p>\n\n<pre><strong>输入:</strong>nums = [0,1,0,1,1,0,0]\n<strong>输出:</strong>1\n<strong>解释:</strong>这里列出一些能够将所有 1 聚集在一起的方案:\n[0,<strong><em>0</em></strong>,<em><strong>1</strong></em>,1,1,0,0] 交换 1 次。\n[0,1,<em><strong>1</strong></em>,1,<em><strong>0</strong></em>,0,0] 交换 1 次。\n[1,1,0,0,0,0,1] 交换 2 次(利用数组的环形特性)。\n无法在交换 0 次的情况下将数组中的所有 1 聚集在一起。\n因此需要的最少交换次数为 1 。\n</pre>\n\n<p><strong>示例 2</strong></p>\n\n<pre><strong>输入:</strong>nums = [0,1,1,1,0,0,1,1,0]\n<strong>输出:</strong>2\n<strong>解释:</strong>这里列出一些能够将所有 1 聚集在一起的方案:\n[1,1,1,0,0,0,0,1,1] 交换 2 次(利用数组的环形特性)。\n[1,1,1,1,1,0,0,0,0] 交换 2 次。\n无法在交换 0 次或 1 次的情况下将数组中的所有 1 聚集在一起。\n因此需要的最少交换次数为 2 。\n</pre>\n\n<p><strong>示例 3</strong></p>\n\n<pre><strong>输入:</strong>nums = [1,1,0,0,1]\n<strong>输出:</strong>0\n<strong>解释:</strong>得益于数组的环形特性,所有的 1 已经聚集在一起。\n因此需要的最少交换次数为 0 。</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>nums[i]</code> 为 <code>0</code> 或者 <code>1</code></li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 34,
"likes": 35,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
@@ -143,7 +143,7 @@
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"5.4K\", \"totalSubmission\": \"11.4K\", \"totalAcceptedRaw\": 5361, \"totalSubmissionRaw\": 11444, \"acRate\": \"46.8%\"}",
"stats": "{\"totalAccepted\": \"5.6K\", \"totalSubmission\": \"11.8K\", \"totalAcceptedRaw\": 5561, \"totalSubmissionRaw\": 11835, \"acRate\": \"47.0%\"}",
"hints": [
"Notice that the number of 1s to be grouped together is fixed. It is the number of 1's the whole array has.",
"Call this number total. We should then check for every subarray of size total (possibly wrapped around), how many swaps are required to have the subarray be all 1s.",