{"id":9424,"date":"2022-01-17T17:13:22","date_gmt":"2022-01-18T01:13:22","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9424"},"modified":"2022-01-17T17:16:52","modified_gmt":"2022-01-18T01:16:52","slug":"leetcode-2134-minimum-swaps-to-group-all-1s-together-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/sliding-window\/leetcode-2134-minimum-swaps-to-group-all-1s-together-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2134. Minimum Swaps to Group All 1&#8217;s Together II"},"content":{"rendered":"\n<p>A&nbsp;<strong>swap<\/strong>&nbsp;is defined as taking two&nbsp;<strong>distinct<\/strong>&nbsp;positions in an array and swapping the values in them.<\/p>\n\n\n\n<p>A&nbsp;<strong>circular<\/strong>&nbsp;array is defined as an array where we consider the&nbsp;<strong>first<\/strong>&nbsp;element and the&nbsp;<strong>last<\/strong>&nbsp;element to be&nbsp;<strong>adjacent<\/strong>.<\/p>\n\n\n\n<p>Given a&nbsp;<strong>binary<\/strong>&nbsp;<strong>circular<\/strong>&nbsp;array&nbsp;<code>nums<\/code>, return&nbsp;<em>the minimum number of swaps required to group all&nbsp;<\/em><code>1<\/code><em>&#8216;s present in the array together at&nbsp;<strong>any location<\/strong><\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [0,1,0,1,1,0,0]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> Here are a few of the ways to group all the 1's together:\n[0,0,1,1,1,0,0] using 1 swap.\n[0,1,1,1,0,0,0] using 1 swap.\n[1,1,0,0,0,0,1] using 2 swaps (using the circular property of the array).\nThere is no way to group all 1's together with 0 swaps.\nThus, the minimum number of swaps required is 1.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [0,1,1,1,0,0,1,1,0]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> Here are a few of the ways to group all the 1's together:\n[1,1,1,0,0,0,0,1,1] using 2 swaps (using the circular property of the array).\n[1,1,1,1,1,0,0,0,0] using 2 swaps.\nThere is no way to group all 1's together with 0 or 1 swaps.\nThus, the minimum number of swaps required is 2.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,1,0,0,1]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> All the 1's are already grouped together due to the circular property of the array.\nThus, the minimum number of swaps required is 0.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>nums[i]<\/code>&nbsp;is either&nbsp;<code>0<\/code>&nbsp;or&nbsp;<code>1<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sliding Window<\/strong><\/h2>\n\n\n\n<p>Step 1: Count how many ones are there in the array. Assume it&#8217;s K.<br>Step 2: For each window of size k, count how many ones in the window, we have to swap 0s out with 1s to fill the window. ans = min(ans, k &#8211; ones).<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minSwaps(vector<int>& nums) {\n    const int n = nums.size();\n    const int k = accumulate(begin(nums), end(nums), 0);\n    int ans = INT_MAX;\n    for (int i = 0, cur = 0; i < n + k; ++i) {\n      if (i >= k) cur -= nums[(i - k + n) % n];\n      cur += nums[i % n];      \n      ans = min(ans, k - cur);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A&nbsp;swap&nbsp;is defined as taking two&nbsp;distinct&nbsp;positions in an array and swapping the values in them. A&nbsp;circular&nbsp;array is defined as an array where we consider the&nbsp;first&nbsp;element and&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[476],"tags":[20,177,215],"class_list":["post-9424","post","type-post","status-publish","format-standard","hentry","category-sliding-window","tag-array","tag-medium","tag-sliding-window","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9424","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9424"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9424\/revisions"}],"predecessor-version":[{"id":9427,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9424\/revisions\/9427"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}