<?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>rotated Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/rotated/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/rotated/</link>
	<description></description>
	<lastBuildDate>Mon, 29 Nov 2021 08:48:19 +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>rotated Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/rotated/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 81. Search in Rotated Sorted Array II</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-81-search-in-rotated-sorted-array-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-81-search-in-rotated-sorted-array-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 29 Nov 2021 08:47:01 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[rotated]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8937</guid>

					<description><![CDATA[<p>There is an integer array&#160;nums&#160;sorted in non-decreasing order (not necessarily with&#160;distinct&#160;values). Before being passed to your function,&#160;nums&#160;is&#160;rotated&#160;at an unknown pivot index&#160;k&#160;(0 &#60;= k &#60; nums.length)&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-81-search-in-rotated-sorted-array-ii/">花花酱 LeetCode 81. Search in Rotated Sorted Array II</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>There is an integer array&nbsp;<code>nums</code>&nbsp;sorted in non-decreasing order (not necessarily with&nbsp;<strong>distinct</strong>&nbsp;values).</p>



<p>Before being passed to your function,&nbsp;<code>nums</code>&nbsp;is&nbsp;<strong>rotated</strong>&nbsp;at an unknown pivot index&nbsp;<code>k</code>&nbsp;(<code>0 &lt;= k &lt; nums.length</code>) such that the resulting array is&nbsp;<code>[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]</code>&nbsp;(<strong>0-indexed</strong>). For example,&nbsp;<code>[0,1,2,4,4,4,5,6,6,7]</code>&nbsp;might be rotated at pivot index&nbsp;<code>5</code>&nbsp;and become&nbsp;<code>[4,5,6,6,7,0,1,2,4,4]</code>.</p>



<p>Given the array&nbsp;<code>nums</code>&nbsp;<strong>after</strong>&nbsp;the rotation and an integer&nbsp;<code>target</code>, return&nbsp;<code>true</code><em>&nbsp;if&nbsp;</em><code>target</code><em>&nbsp;is in&nbsp;</em><code>nums</code><em>, or&nbsp;</em><code>false</code><em>&nbsp;if it is not in&nbsp;</em><code>nums</code><em>.</em></p>



<p>You must decrease the overall operation steps as much as possible.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,5,6,0,0,1,2], target = 0
<strong>Output:</strong> true
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,5,6,0,0,1,2], target = 3
<strong>Output:</strong> false
</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 5000</code></li><li><code>-10<sup>4</sup>&nbsp;&lt;= nums[i] &lt;= 10<sup>4</sup></code></li><li><code>nums</code>&nbsp;is guaranteed to be rotated at some pivot.</li><li><code>-10<sup>4</sup>&nbsp;&lt;= target &lt;= 10<sup>4</sup></code></li></ul>



<h2><strong>Solution: Binary search or divide and conquer</strong></h2>



<p>If current range is ordered, use binary search, Otherwise, divide and conquer.</p>



<p>Time complexity: O(logn) best, O(n) worst<br>Space complexity: O(logn)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  bool search(vector&lt;int&gt;&amp; A, int target) {
    return search(A, 0, A.size() - 1, target);
  }
private:
  bool search(vector&lt;int&gt;&amp; A, int l, int r, int target) {
    if (l &gt; r) return 0;
    if (l == r) return target == A[l];

    int mid = l + (r - l) / 2;

    if (A[l] &lt; A[mid] &amp;&amp; A[mid] &lt; A[r])
      return (target &lt;= A[mid]) ? search(A, l, mid, target) : search(A, mid + 1, r, target);
    else
      return search(A, l, mid, target) || search(A, mid + 1, r, target);
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-81-search-in-rotated-sorted-array-ii/">花花酱 LeetCode 81. Search in Rotated Sorted Array II</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/binary-search/leetcode-81-search-in-rotated-sorted-array-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 33. Search in Rotated Sorted Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-33-search-in-rotated-sorted-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-33-search-in-rotated-sorted-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 27 Nov 2021 01:51:32 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[rotated]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8793</guid>

					<description><![CDATA[<p>There is an integer array&#160;nums&#160;sorted in ascending order (with&#160;distinct&#160;values). Prior to being passed to your function,&#160;nums&#160;is&#160;possibly rotated&#160;at an unknown pivot index&#160;k&#160;(1 &#60;= k &#60; nums.length)&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-33-search-in-rotated-sorted-array/">花花酱 LeetCode 33. Search in Rotated Sorted 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[
<p>There is an integer array&nbsp;<code>nums</code>&nbsp;sorted in ascending order (with&nbsp;<strong>distinct</strong>&nbsp;values).</p>



<p>Prior to being passed to your function,&nbsp;<code>nums</code>&nbsp;is&nbsp;<strong>possibly rotated</strong>&nbsp;at an unknown pivot index&nbsp;<code>k</code>&nbsp;(<code>1 &lt;= k &lt; nums.length</code>) such that the resulting array is&nbsp;<code>[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]</code>&nbsp;(<strong>0-indexed</strong>). For example,&nbsp;<code>[0,1,2,4,5,6,7]</code>&nbsp;might be rotated at pivot index&nbsp;<code>3</code>&nbsp;and become&nbsp;<code>[4,5,6,7,0,1,2]</code>.</p>



<p>Given the array&nbsp;<code>nums</code>&nbsp;<strong>after</strong>&nbsp;the possible rotation and an integer&nbsp;<code>target</code>, return&nbsp;<em>the index of&nbsp;</em><code>target</code><em>&nbsp;if it is in&nbsp;</em><code>nums</code><em>, or&nbsp;</em><code>-1</code><em>&nbsp;if it is not in&nbsp;</em><code>nums</code>.</p>



<p>You must write an algorithm with&nbsp;<code>O(log n)</code>&nbsp;runtime complexity.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [4,5,6,7,0,1,2], target = 0
<strong>Output:</strong> 4
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [4,5,6,7,0,1,2], target = 3
<strong>Output:</strong> -1
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1], target = 0
<strong>Output:</strong> -1
</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 5000</code></li><li><code>-10<sup>4</sup>&nbsp;&lt;= nums[i] &lt;= 10<sup>4</sup></code></li><li>All values of&nbsp;<code>nums</code>&nbsp;are&nbsp;<strong>unique</strong>.</li><li><code>nums</code>&nbsp;is an ascending array that is possibly rotated.</li><li><code>-10<sup>4</sup>&nbsp;&lt;= target &lt;= 10<sup>4</sup></code></li></ul>



<h2><strong>Solution: Binary Search</strong></h2>



<p>If the current range [l, r] is ordered, reduce to normal binary search. Otherwise, determine the range to search next by comparing target and nums[0].</p>



<p>Time complexity: O(logn)<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:
  int search(vector&lt;int&gt;&amp; nums, int target) {
    int l = 0;
    int r = nums.size();
    while (l &lt; r) {
      int m = l + (r - l) / 2;
      int x = (nums[m] &lt; nums[0]) == (target &lt; nums[0])
              ? nums[m]
              : target &lt; nums[0] ? INT_MIN : INT_MAX;
      if (x &lt; target)
        l = m + 1;
      else if (x &gt; target)
        r = m;
      else
        return m;
    }
    return -1;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-33-search-in-rotated-sorted-array/">花花酱 LeetCode 33. Search in Rotated Sorted 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/binary-search/leetcode-33-search-in-rotated-sorted-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
