<?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>rotation Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/rotation/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/rotation/</link>
	<description></description>
	<lastBuildDate>Mon, 09 Aug 2021 03:22:56 +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>rotation Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/rotation/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1886. Determine Whether Matrix Can Be Obtained By Rotation</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-1886-determine-whether-matrix-can-be-obtained-by-rotation/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-1886-determine-whether-matrix-can-be-obtained-by-rotation/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 09 Aug 2021 03:22:13 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[inplace]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[rotation]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8537</guid>

					<description><![CDATA[<p>Given two&#160;n x n&#160;binary matrices&#160;mat&#160;and&#160;target, return&#160;true&#160;if it is possible to make&#160;mat&#160;equal to&#160;target&#160;by&#160;rotating&#160;mat&#160;in&#160;90-degree increments, or&#160;false&#160;otherwise. Example 1: Input: mat = [[0,1],[1,0]], target = [[1,0],[0,1]] Output: true&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1886-determine-whether-matrix-can-be-obtained-by-rotation/">花花酱 LeetCode 1886. Determine Whether Matrix Can Be Obtained By Rotation</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>Given two&nbsp;<code>n x n</code>&nbsp;binary matrices&nbsp;<code>mat</code>&nbsp;and&nbsp;<code>target</code>, return&nbsp;<code>true</code><em>&nbsp;if it is possible to make&nbsp;</em><code>mat</code><em>&nbsp;equal to&nbsp;</em><code>target</code><em>&nbsp;by&nbsp;<strong>rotating</strong>&nbsp;</em><code>mat</code><em>&nbsp;in&nbsp;<strong>90-degree increments</strong>, or&nbsp;</em><code>false</code><em>&nbsp;otherwise.</em></p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/05/20/grid3.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> mat = [[0,1],[1,0]], target = [[1,0],[0,1]]
<strong>Output:</strong> true
<strong>Explanation: </strong>We can rotate mat 90 degrees clockwise to make mat equal target.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/05/20/grid4.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> mat = [[0,1],[1,1]], target = [[1,0],[0,1]]
<strong>Output:</strong> false
<strong>Explanation:</strong> It is impossible to make mat equal to target by rotating mat.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/05/26/grid4.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> mat = [[0,0,0],[0,1,0],[1,1,1]], target = [[1,1,1],[0,1,0],[0,0,0]]
<strong>Output:</strong> true
<strong>Explanation: </strong>We can rotate mat 90 degrees clockwise two times to make mat equal target.
</pre>



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



<ul><li><code>n == mat.length == target.length</code></li><li><code>n == mat[i].length == target[i].length</code></li><li><code>1 &lt;= n &lt;= 10</code></li><li><code>mat[i][j]</code>&nbsp;and&nbsp;<code>target[i][j]</code>&nbsp;are either&nbsp;<code>0</code>&nbsp;or&nbsp;<code>1</code>.</li></ul>



<h2><strong>Solution: Simulation</strong></h2>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  bool findRotation(vector&lt;vector&lt;int&gt;&gt;&amp; mat, vector&lt;vector&lt;int&gt;&gt;&amp; target) {    
    const int n = mat.size();
    auto rot = [n](vector&lt;vector&lt;int&gt;&gt;&amp; mat) {      
      for (int i = 0; i &lt; n; ++i)
        for (int j = i; j &lt; n; ++j)
          swap(mat[i][j], mat[j][i]);
      for (int j = 0; j &lt; n; j++)
        for (int i = 0; i &lt; n / 2; ++i)
          swap(mat[i][j], mat[n - i - 1][j]);
      return mat;
    };
    for (int i = 0; i &lt; 4; ++i)
      if (rot(mat) == target) return true;    
    return false;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1886-determine-whether-matrix-can-be-obtained-by-rotation/">花花酱 LeetCode 1886. Determine Whether Matrix Can Be Obtained By Rotation</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/simulation/leetcode-1886-determine-whether-matrix-can-be-obtained-by-rotation/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1752. Check if Array Is Sorted and Rotated</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1752-check-if-array-is-sorted-and-rotated/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1752-check-if-array-is-sorted-and-rotated/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 07 Feb 2021 16:07:02 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[rotation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8079</guid>

					<description><![CDATA[<p>Given an array&#160;nums, return&#160;true&#160;if the array was originally sorted in non-decreasing order, then rotated&#160;some&#160;number of positions (including zero). Otherwise, return&#160;false. There may be&#160;duplicates&#160;in the original&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1752-check-if-array-is-sorted-and-rotated/">花花酱 LeetCode 1752. Check if Array Is Sorted and Rotated</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>Given an array&nbsp;<code>nums</code>, return&nbsp;<code>true</code><em>&nbsp;if the array was originally sorted in non-decreasing order, then rotated&nbsp;<strong>some</strong>&nbsp;number of positions (including zero)</em>. Otherwise, return&nbsp;<code>false</code>.</p>



<p>There may be&nbsp;<strong>duplicates</strong>&nbsp;in the original array.</p>



<p><strong>Note:</strong>&nbsp;An array&nbsp;<code>A</code>&nbsp;rotated by&nbsp;<code>x</code>&nbsp;positions results in an array&nbsp;<code>B</code>&nbsp;of the same length such that&nbsp;<code>A[i] == B[(i+x) % A.length]</code>, where&nbsp;<code>%</code>&nbsp;is the modulo operation.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [3,4,5,1,2]
<strong>Output:</strong> true
<strong>Explanation:</strong> [1,2,3,4,5] is the original sorted array.
You can rotate the array by x = 3 positions to begin on the the element of value 3: [3,4,5,1,2].
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,1,3,4]
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no sorted array once rotated that can make nums.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> true
<strong>Explanation:</strong> [1,2,3] is the original sorted array.
You can rotate the array by x = 0 positions (i.e. no rotation) to make nums.
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> true
<strong>Explanation:</strong> [1,1,1] is the original sorted array.
You can rotate any number of positions to make nums.
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,1]
<strong>Output:</strong> true
<strong>Explanation:</strong> [1,2] is the original sorted array.
You can rotate the array by x = 5 positions to begin on the element of value 2: [2,1].
</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 100</code></li><li><code>1 &lt;= nums[i] &lt;= 100</code></li></ul>



<h2><strong>Solution: Counting and checking</strong></h2>



<p>Count how many turning points (nums[i] &lt; nums[i &#8211; 1]) in the array. Return false if there are more than 1.<br>For the turning point r, (nums[r] &lt; nums[r &#8211; 1), return true if both of the following conditions are satisfied: <br>1. nums[r &#8211; 1] is the largest number, e.g. nums[r &#8211; 1] &gt;= nums[n &#8211; 1]<br>2. nums[r] is the smallest number, e.g. nums[r] &lt;= nums[0]</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  bool check(vector&lt;int&gt;&amp; nums) {
    int count = 0;
    int r = -1;
    for (size_t i = 1; i &lt; nums.size(); ++i)
      if (nums[i] &lt; nums[i - 1]) {
        ++count;
        r = i;
      }
    if (count == 0) return true;
    if (count &gt; 1) return false;     
    return nums[r] &lt;= nums[0] &amp;&amp; nums[r - 1] &gt;= nums.back();
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1752-check-if-array-is-sorted-and-rotated/">花花酱 LeetCode 1752. Check if Array Is Sorted and Rotated</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/algorithms/array/leetcode-1752-check-if-array-is-sorted-and-rotated/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1238. Circular Permutation in Binary Representation</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1238-circular-permutation-in-binary-representation/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1238-circular-permutation-in-binary-representation/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 28 Oct 2019 03:22:14 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[gray code]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[rotation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5788</guid>

					<description><![CDATA[<p>Given 2 integers&#160;n&#160;and&#160;start. Your task is return&#160;any&#160;permutation&#160;p&#160;of&#160;(0,1,2.....,2^n -1)&#160;such that : p[0] = start p[i]&#160;and&#160;p[i+1]&#160;differ by only one bit in their binary representation. p[0]&#160;and&#160;p[2^n -1]&#160;must also&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1238-circular-permutation-in-binary-representation/">花花酱 LeetCode 1238. Circular Permutation in Binary Representation</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>Given 2 integers&nbsp;<code>n</code>&nbsp;and&nbsp;<code>start</code>. Your task is return&nbsp;<strong>any</strong>&nbsp;permutation&nbsp;<code>p</code>&nbsp;of&nbsp;<code>(0,1,2.....,2^n -1)&nbsp;</code>such that :</p>



<ul><li><code>p[0] = start</code></li><li><code>p[i]</code>&nbsp;and&nbsp;<code>p[i+1]</code>&nbsp;differ by only one bit in their binary representation.</li><li><code>p[0]</code>&nbsp;and&nbsp;<code>p[2^n -1]</code>&nbsp;must also differ by only one bit in their binary representation.</li></ul>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 2, start = 3
<strong>Output:</strong> [3,2,0,1]
<strong>Explanation:</strong> The binary representation of the permutation is (11,10,00,01). 
All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 3, start = 2
<strong>Output:</strong> [2,6,7,5,4,0,1,3]
<strong>Explanation:</strong> The binary representation of the permutation is (010,110,111,101,100,000,001,011).
</pre>



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



<ul><li><code>1 &lt;= n &lt;= 16</code></li><li><code>0 &lt;= start&nbsp;&lt;&nbsp;2 ^ n</code></li></ul>



<p>Solution 1: Gray Code (DP) + Rotation</p>



<p>Gray code starts with 0, need to rotate after generating the list.<br><br>Time complexity: O(2^n)<br>Space complexity: O(2^n)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; circularPermutation(int n, int start) {
    vector&lt;vector&lt;int&gt;&gt; dp(n + 1);
    dp[0] = {0};    
    for (int i = 1; i &lt;= n; ++i) {
      dp[i] = dp[i - 1];
      for (int j = dp[i - 1].size() - 1; j &gt;= 0; --j)
        dp[i].push_back(dp[i - 1][j] | (1 &lt;&lt; (i - 1)));
    }
    for (auto it = begin(dp[n]); it != end(dp[n]); ++it)
      if (*it == start) {
        rotate(begin(dp[n]), it, end(dp[n]));
        break;
      }
    return dp[n];
  }
};</pre>
</div></div>



<h2><strong>Solution 2: Gray code with a start</strong></h2>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; circularPermutation(int n, int start) {
    vector&lt;int&gt; ans(1 &lt;&lt; n);
    for (int i = 0; i &lt; 1 &lt;&lt; n; ++i)
      ans[i] = start ^ i ^ i &gt;&gt; 1;
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1238-circular-permutation-in-binary-representation/">花花酱 LeetCode 1238. Circular Permutation in Binary Representation</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/dynamic-programming/leetcode-1238-circular-permutation-in-binary-representation/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 899. Orderly Queue</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-899-orderly-queue/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-899-orderly-queue/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 02 Sep 2018 06:07:48 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[rotation]]></category>
		<category><![CDATA[sort]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3806</guid>

					<description><![CDATA[<p>Problem A string S of lowercase letters is given.  Then, we may make any number of moves. In each move, we choose one of the first K letters (starting from the left),&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-899-orderly-queue/">花花酱 LeetCode 899. Orderly Queue</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="question-description__3U1T">
<h1><strong>Problem</strong></h1>
<p>A string <code>S</code> of lowercase letters is given.  Then, we may make any number of <em>moves</em>.</p>
<p>In each move, we choose one of the first <code>K</code> letters (starting from the left), remove it, and place it at the end of the string.</p>
<p>Return the lexicographically smallest string we could have after any number of moves.</p>
<div>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>S = <span id="example-input-1-1">"cba"</span>, K = <span id="example-input-1-2">1</span>
<strong>Output: </strong><span id="example-output-1">"acb"</span>
<strong>Explanation: </strong>
In the first move, we move the 1st character ("c") to the end, obtaining the string "bac".
In the second move, we move the 1st character ("b") to the end, obtaining the final result "acb".
</pre>
<div>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>S = <span id="example-input-2-1">"baaca"</span>, K = <span id="example-input-2-2">3</span>
<strong>Output: </strong><span id="example-output-2">"aaabc"</span>
<strong>Explanation: </strong>
In the first move, we move the 1st character ("b") to the end, obtaining the string "aacab".
In the second move, we move the 3rd character ("c") to the end, obtaining the final result "aaabc".
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= K &lt;= S.length &lt;= 1000</code></li>
<li><code>S</code> consists of lowercase letters only.</li>
</ol>
<h1><strong>Solution: Rotation or Sort?</strong></h1>
<p>if \(k =1\), we can only rotate the string.</p>
<p>if \(k &gt; 1\), we can bubble sort the string.</p>
<p>Time complexity: O(n^2)</p>
<p>Space complexity: O(n)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 4 ms
class Solution {
public:
  string orderlyQueue(string S, int K) {
    if (K &gt; 1) {
      sort(begin(S), end(S));
      return S;
    }
    
    string ans = S;
    for (int i = 1; i &lt; S.size(); ++i)
      ans = min(ans, S.substr(i) +  S.substr(0, i));
    return ans;
  }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
  public String orderlyQueue(String S, int K) {
    if (K &gt; 1) {
      char[] chars = S.toCharArray();
      Arrays.sort(chars);
      return new String(chars);
    }
    
    String ans = S;
    for (int i = 1; i &lt; S.length(); ++i) {
      String tmp = S.substring(i) + S.substring(0, i);
      if (tmp.compareTo(ans) &lt; 0) ans = tmp;        
    }
    return ans;
  }
}</pre><p></div><h2 class="tabtitle">Python3 SC O(n^2)</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag"># Author: Huahua 48 ms
class Solution:
  def orderlyQueue(self, S, K):
    if K &gt; 1: return ''.join(sorted(S))
    return min([S[i:] + S[0:i] for i in range(0, len(S))])</pre><p></div><h2 class="tabtitle">Python3 SC O(n)</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag"># Author: Huahua 36 ms
class Solution:
  def orderlyQueue(self, S, K):
    if K &gt; 1: return ''.join(sorted(S))
    ans = S
    for i in range(0, len(S)):
      ans = min(ans, S[i:] + S[0:i])
    return ans</pre><p></div></div></p>
</div>
</div>
</div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-899-orderly-queue/">花花酱 LeetCode 899. Orderly Queue</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/string/leetcode-899-orderly-queue/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 189. Rotate Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-189-rotate-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-189-rotate-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 06 Aug 2018 15:22:05 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[reverse]]></category>
		<category><![CDATA[rotation]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3456</guid>

					<description><![CDATA[<p>Problem Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: [1,2,3,4,5,6,7] and k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-189-rotate-array/">花花酱 LeetCode 189. Rotate Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>Given an array, rotate the array to the right by <em>k</em> steps, where <em>k</em> is non-negative.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false "><strong>Input:</strong> [1,2,3,4,5,6,7] and k = 3 
<strong>Output:</strong> [5,6,7,1,2,3,4]
<strong>Explanation:</strong> rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] 
rotate 3 steps to the right: [5,6,7,1,2,3,4]</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input:</strong>[-1,-100,3,99] and k = 2 
<strong>Output:</strong> [3,99,-1,-100] 
<strong>Explanation:</strong> rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100]</pre>
<p><strong>Note:</strong></p>
<ul>
<li>Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.</li>
<li>Could you do it in-place with O(1) extra space?</li>
</ul>
<h1><strong>Solution 1: Simulate rotation with three reverses.</strong></h1>
<p>If k &gt;= n, rotating k times has the same effect as rotating k % n times.</p>
<p>[1,2,3,4,5,6,7], K = 3</p>
<p>[5,6,7,1,2,3,4]</p>
<p>We can simulate the rotation with three reverses.</p>
<ol>
<li>reverse the whole array O(n) [<strong><span style="color: #ff0000;">7,6,5</span></strong>,<strong><span style="color: #000080;">4,3,2,1</span></strong>]</li>
<li>reverse the left part 0 ~ k &#8211; 1 O(k) [5,6,7,<strong><span style="color: #000080;">4,3,2,1]</span></strong></li>
<li>reverse the right part k ~ n &#8211; 1 O(n-k) [5,6,7,1,2,3,4]</li>
</ol>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1) in-place</p>
<p>C++</p><pre class="crayon-plain-tag">class Solution {
public:
  void rotate(vector&lt;int&gt;&amp; nums, int k) {
    if (nums.empty()) return;
    k %= nums.size();
    if (k == 0) return;
    reverse(begin(nums), end(nums));
    reverse(begin(nums), begin(nums) + k);
    reverse(begin(nums) + k, end(nums));
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-189-rotate-array/">花花酱 LeetCode 189. Rotate Array</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/algorithms/array/leetcode-189-rotate-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
