<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>consecutive Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/consecutive/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/consecutive/</link>
	<description></description>
	<lastBuildDate>Sun, 27 Dec 2020 08:10:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>consecutive Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/consecutive/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1703. Minimum Adjacent Swaps for K Consecutive Ones</title>
		<link>https://zxi.mytechroad.com/blog/sliding-window/leetcode-1703-minimum-adjacent-swaps-for-k-consecutive-ones/</link>
					<comments>https://zxi.mytechroad.com/blog/sliding-window/leetcode-1703-minimum-adjacent-swaps-for-k-consecutive-ones/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 27 Dec 2020 07:56:26 +0000</pubDate>
				<category><![CDATA[Sliding Window]]></category>
		<category><![CDATA[consecutive]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[prefix sum]]></category>
		<category><![CDATA[sliding window]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7853</guid>

					<description><![CDATA[<p>You are given an integer array,&#160;nums, and an integer&#160;k.&#160;nums&#160;comprises of only&#160;0&#8216;s and&#160;1&#8216;s. In one move, you can choose two&#160;adjacent&#160;indices and swap their values. Return&#160;the&#160;minimum&#160;number of&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sliding-window/leetcode-1703-minimum-adjacent-swaps-for-k-consecutive-ones/">花花酱 LeetCode 1703. Minimum Adjacent Swaps for K Consecutive Ones</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You are given an integer array,&nbsp;<code>nums</code>, and an integer&nbsp;<code>k</code>.&nbsp;<code>nums</code>&nbsp;comprises of only&nbsp;<code>0</code>&#8216;s and&nbsp;<code>1</code>&#8216;s. In one move, you can choose two&nbsp;<strong>adjacent</strong>&nbsp;indices and swap their values.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>minimum</strong>&nbsp;number of moves required so that&nbsp;</em><code>nums</code><em>&nbsp;has&nbsp;</em><code>k</code><em>&nbsp;<strong>consecutive</strong>&nbsp;</em><code>1</code><em>&#8216;s</em>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,0,0,1,0,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> In 1 move, nums could be [1,0,0,0,1,1] and have 2 consecutive 1's.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,0,0,0,0,0,1,1], k = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> In 5 moves, the leftmost 1 can be shifted right until nums = [0,0,0,0,0,1,1,1].
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,1,0,1], k = 2
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums already has 2 consecutive 1's.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li><li><code>nums[i]</code>&nbsp;is&nbsp;<code>0</code>&nbsp;or&nbsp;<code>1</code>.</li><li><code>1 &lt;= k &lt;= sum(nums)</code></li></ul>



<h2><strong>Solution: Prefix Sum + Sliding Window</strong></h2>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



<p>We only care positions of 1s, we can move one element from position x to y (assuming x + 1 ~ y are all zeros) in y &#8211; x steps. e.g. [0 0 1 0 0 0 1] =&gt; [0 0 0 0 0 1 1], move first 1 at position 2 to position 5, cost is 5 &#8211; 2 = 3.</p>



<p>Given a size k window of indices of ones, the optimal solution it to use the median number as center. We can compute the cost to form consecutive numbers:</p>



<p>e.g. [1 4 7 9 10] =&gt; [5 6 7 8 9] cost = (5 &#8211; 1) + (6 &#8211; 4) + (9 &#8211; 8) + (10 &#8211; 9) = 8</p>



<p>However, naive solution takes O(n*k) =&gt; TLE.</p>



<p>We can use prefix sum to compute the cost of a window in O(1) to reduce time complexity to O(n)</p>



<p>First, in order to use sliding window, we change the target of every number in the window to the median number.<br>e.g. [1 4 7 9 10] =&gt; [7 7 7 7 7] cost = (7 &#8211; 1) + (7 &#8211; 4) + (7 &#8211; 7) + (9 &#8211; 7) + (10 &#8211; 7) = (9 + 10) &#8211; (1 + 4) = right &#8211; left. <br>[5 6 7 8 9] =&gt; [7 7 7 7 7] takes extra 2 + 1 + 1 + 2 = 6 steps =  (k / 2) * ((k + 1) / 2), these extra steps should be deducted from the final answer. </p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution {
public:
  int minMoves(vector&lt;int&gt;&amp; nums, int k) {
    vector&lt;long&gt; s(1);
    for (int i = 0; i &lt; nums.size(); ++i)
      if (nums[i])
        s.push_back(s.back() + i);
    const int n = s.size();
    long ans = 1e10;
    const int m1 = k / 2, m2 = (k + 1) / 2;
    for (int i = 0; i + k &lt; n; ++i) {
      const long right = s[i + k] - s[i + m1];
      const long left = s[i + m2] - s[i];
      ans = min(ans, right - left);
    }
    return ans - m1 * m2;
  }
};</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution:
  def minMoves(self, nums: List[int], k: int) -&gt; int:
    ans = 1e10
    s = [0]    
    for i, v in enumerate(nums):
      if v: s.append(s[-1] + i)
    n = len(s)
    m1 = k // 2
    m2 = (k + 1) // 2
    for i in range(n - k):
      right = s[i + k] - s[i + m1]
      left = s[i + m2] - s[i]
      ans = min(ans, right - left)
    return ans - m1 * m2</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sliding-window/leetcode-1703-minimum-adjacent-swaps-for-k-consecutive-ones/">花花酱 LeetCode 1703. Minimum Adjacent Swaps for K Consecutive Ones</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/sliding-window/leetcode-1703-minimum-adjacent-swaps-for-k-consecutive-ones/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
