<?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>sorted Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/sorted/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/sorted/</link>
	<description></description>
	<lastBuildDate>Sun, 20 Jan 2019 08:41:09 +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>sorted Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/sorted/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 977. Squares of a Sorted Array</title>
		<link>https://zxi.mytechroad.com/blog/two-pointers/leetcode-977-squares-of-a-sorted-array/</link>
					<comments>https://zxi.mytechroad.com/blog/two-pointers/leetcode-977-squares-of-a-sorted-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 20 Jan 2019 08:24:32 +0000</pubDate>
				<category><![CDATA[Two pointers]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[sorted]]></category>
		<category><![CDATA[two pointers]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4663</guid>

					<description><![CDATA[<p>Given an array of integers&#160;A&#160;sorted in non-decreasing order,&#160;return an array of the squares of each number,&#160;also in sorted non-decreasing order. Example 1: Input: [-4,-1,0,3,10] Output:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/two-pointers/leetcode-977-squares-of-a-sorted-array/">花花酱 LeetCode 977. Squares of a 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>Given an array of integers&nbsp;<code>A</code>&nbsp;sorted in non-decreasing order,&nbsp;return an array of the squares of each number,&nbsp;also in sorted non-decreasing order.</p>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[-4,-1,0,3,10]
<strong>Output: </strong>[0,1,9,16,100]
</pre>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[-7,-3,2,3,11]
<strong>Output: </strong>[4,9,9,49,121]
</pre>



<p><strong>Note:</strong></p>



<ol><li><code>1 &lt;= A.length &lt;= 10000</code></li><li><code>-10000 &lt;= A[i] &lt;= 10000</code></li><li><code>A</code>&nbsp;is sorted in non-decreasing order.</li></ol>



<h2>Solution: Two pointers + Merge two sorted arrays</h2>



<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, running time: 120 ms
class Solution {
public:
  vector&lt;int&gt; sortedSquares(vector&lt;int&gt;&amp; A) {
    vector&lt;int&gt; ans(A.size());
    auto e = prev(end(A));
    auto s = begin(A);
    auto it = end(ans);
    while (--it &gt;= begin(ans)) {
      if (e &gt; s &amp;&amp; abs(*e) &gt; abs(*s))
        *it = pow(*e--, 2);        
      else
        *it = pow(*s++, 2);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/two-pointers/leetcode-977-squares-of-a-sorted-array/">花花酱 LeetCode 977. Squares of a 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/two-pointers/leetcode-977-squares-of-a-sorted-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 220. Contains Duplicate III</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-220-contains-duplicate-iii/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-220-contains-duplicate-iii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 21 Dec 2018 05:55:24 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[multiset]]></category>
		<category><![CDATA[sliding window]]></category>
		<category><![CDATA[sorted]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4515</guid>

					<description><![CDATA[<p>Given an array of integers, find out whether there are two distinct indices&#160;i&#160;and&#160;j&#160;in the array such that the&#160;absolute&#160;difference between&#160;nums[i]&#160;and&#160;nums[j]&#160;is at most&#160;t&#160;and the&#160;absolute&#160;difference between&#160;i&#160;and&#160;j&#160;is at most&#160;k.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-220-contains-duplicate-iii/">花花酱 LeetCode 220. Contains Duplicate III</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 of integers, find out whether there are two distinct indices&nbsp;<em>i</em>&nbsp;and&nbsp;<em>j</em>&nbsp;in the array such that the&nbsp;<strong>absolute</strong>&nbsp;difference between&nbsp;<strong>nums[i]</strong>&nbsp;and&nbsp;<strong>nums[j]</strong>&nbsp;is at most&nbsp;<em>t</em>&nbsp;and the&nbsp;<strong>absolute</strong>&nbsp;difference between&nbsp;<em>i</em>&nbsp;and&nbsp;<em>j</em>&nbsp;is at most&nbsp;<em>k</em>.</p>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>nums = [1,2,3,1], k = 3, t = 0<br><strong>Output: </strong>true</pre>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>nums = [1,0,1,1], k = 1, t = 2<br><strong>Output: </strong>true</pre>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>nums = [1,5,9,1,5,9], k = 2, t = 3<br><strong>Output: </strong>false</pre>



<h1><strong>Solution: Sliding Window + Multiset (OrderedSet)</strong></h1>



<p>Maintaining a sliding window of sorted numbers of k + 1. After the i-th number was inserted into the sliding window, check whether its left and right neighbors satisfy abs(nums[i] &#8211; neighbor) &lt;= t</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua, running time: 12 ms
class Solution {
public:
  bool containsNearbyAlmostDuplicate(vector&lt;int&gt;&amp; nums, int k, int t) {
    multiset&lt;long&gt; s;
    for (int i = 0; i &lt; nums.size(); ++i) {
      if (i &gt; k)
        s.erase(s.find(nums[i - k - 1]));
            
      auto it = s.insert(nums[i]);
      if (it != begin(s) &amp;&amp; *it - *prev(it) &lt;= t)
        return true;
      if (next(it) != end(s) &amp;&amp; *next(it) - *it &lt;= t)
        return true;
    }
    return false;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-220-contains-duplicate-iii/">花花酱 LeetCode 220. Contains Duplicate III</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/hashtable/leetcode-220-contains-duplicate-iii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 744. Find Smallest Letter Greater Than Target</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-744-find-smallest-letter-greater-than-target/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-744-find-smallest-letter-greater-than-target/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 11 Dec 2017 15:44:02 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[sorted]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1176</guid>

					<description><![CDATA[<p>Problem: Given a list of sorted characters letters containing only lowercase letters, and given a target letter target, find the smallest element in the list that is larger&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-744-find-smallest-letter-greater-than-target/">花花酱 LeetCode 744. Find Smallest Letter Greater Than Target</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><iframe width="500" height="375" src="https://www.youtube.com/embed/nxJXnQ7wLhM?feature=oembed" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>Given a list of sorted characters <code>letters</code> containing only lowercase letters, and given a target letter <code>target</code>, find the smallest element in the list that is larger than the given target.</p>
<p>Letters also wrap around. For example, if the target is <code>target = 'z'</code> and <code>letters = ['a', 'b']</code>, the answer is <code>'a'</code>.</p>
<p><b>Examples:</b></p><pre class="crayon-plain-tag">Input:
letters = [&quot;c&quot;, &quot;f&quot;, &quot;j&quot;]
target = &quot;a&quot;
Output: &quot;c&quot;

Input:
letters = [&quot;c&quot;, &quot;f&quot;, &quot;j&quot;]
target = &quot;c&quot;
Output: &quot;f&quot;

Input:
letters = [&quot;c&quot;, &quot;f&quot;, &quot;j&quot;]
target = &quot;d&quot;
Output: &quot;f&quot;

Input:
letters = [&quot;c&quot;, &quot;f&quot;, &quot;j&quot;]
target = &quot;g&quot;
Output: &quot;j&quot;

Input:
letters = [&quot;c&quot;, &quot;f&quot;, &quot;j&quot;]
target = &quot;j&quot;
Output: &quot;c&quot;

Input:
letters = [&quot;c&quot;, &quot;f&quot;, &quot;j&quot;]
target = &quot;k&quot;
Output: &quot;c&quot;</pre><p><b>Note:</b></p>
<ol>
<li><code>letters</code> has a length in range <code>[2, 10000]</code>.</li>
<li><code>letters</code> consists of lowercase letters, and contains at least 2 unique letters.</li>
<li><code>target</code> is a lowercase letter.</li>
</ol>
<p><strong>Link: </strong><a href="https://leetcode.com/problems/find-smallest-letter-greater-than-target/description/">https://leetcode.com/problems/find-smallest-letter-greater-than-target/description/ </a></p>
<p><strong>Idea:</strong></p>
<ol>
<li>Scan the array Time complexity: O(n)</li>
<li>Binary search Time complexity: O(logn)</li>
</ol>
<p><img class="alignnone wp-image-1182 size-full" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/12/744-ep128.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/12/744-ep128.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/12/744-ep128-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/12/744-ep128-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p><strong>Solution 1:</strong></p>
<p>C++ / Scan</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 16 ms
class Solution {
public:
    char nextGreatestLetter(vector&lt;char&gt;&amp; letters, char target) {
        for (const char c : letters)
            if (c &gt; target) return c;
        return letters.front();
    }
};</pre><p>C++ / Set</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 16 ms
class Solution {
public:
    char nextGreatestLetter(vector&lt;char&gt;&amp; letters, char target) {
        vector&lt;int&gt; seen(26, 0);
        for (const char c : letters)
            seen[c - 'a'] = 1;
        
        while (true) {
            if (++target &gt; 'z') target = 'a';
            if (seen[target - 'a']) return target;            
        }
        
        return ' ';
    }
};</pre><p>C++ / Binary Search</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 15 ms
class Solution {
public:
    char nextGreatestLetter(vector&lt;char&gt;&amp; letters, char target) {
        int l = 0;
        int r = letters.size();
        while (l &lt; r) {
            const int m = l + (r - l) / 2;
            if (letters[m] &lt;= target)
                l = m + 1;
            else
                r = m;
        }
        return letters[l % letters.size()];
    }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-744-find-smallest-letter-greater-than-target/">花花酱 LeetCode 744. Find Smallest Letter Greater Than Target</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-744-find-smallest-letter-greater-than-target/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 21: Merge Two Sorted Lists</title>
		<link>https://zxi.mytechroad.com/blog/leetcode/leetcode-21-merge-two-sorted-lists/</link>
					<comments>https://zxi.mytechroad.com/blog/leetcode/leetcode-21-merge-two-sorted-lists/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 03 Sep 2017 18:00:03 +0000</pubDate>
				<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[List]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[merge]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[sorted]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=57</guid>

					<description><![CDATA[<p>Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-21-merge-two-sorted-lists/">花花酱 LeetCode 21: Merge Two Sorted Lists</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><iframe width="500" height="375" src="https://www.youtube.com/embed/qckKEYP9bBA?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p>Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.</p>
<p><strong>Solution 1</strong>: Iterative O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
    Solution 1: Iterative
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode dummy(0);
        ListNode* tail=&amp;dummy;
        while(l1 &amp;&amp; l2) {
            if(l1-&gt;val &lt; l2-&gt;val) {
                tail-&gt;next=l1;
                l1=l1-&gt;next;
            }else{
                tail-&gt;next=l2;
                l2=l2-&gt;next;
            }
            tail=tail-&gt;next;
        }
        
        if(l1) tail-&gt;next = l1;
        if(l2) tail-&gt;next = l2;
        
        return dummy.next;
        
    }
};</pre><p>&nbsp;</p>
<p><strong>Solution 2</strong>: Recursive O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        // If one of the list is emptry, return the other one.
        if(!l1 || !l2) return l1 ? l1 : l2;
        // The smaller one becomes the head.
        if(l1-&gt;val &lt; l2-&gt;val) {
            l1-&gt;next = mergeTwoLists(l1-&gt;next, l2);
            return l1;
        } else {
            l2-&gt;next = mergeTwoLists(l1, l2-&gt;next);
            return l2;
        }
    }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-21-merge-two-sorted-lists/">花花酱 LeetCode 21: Merge Two Sorted Lists</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/leetcode/leetcode-21-merge-two-sorted-lists/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
