<?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>mapping Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/mapping/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/mapping/</link>
	<description></description>
	<lastBuildDate>Sun, 09 Dec 2018 09:24:59 +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>mapping Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/mapping/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 953. Verifying an Alien Dictionary</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-953-verifying-an-alien-dictionary/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-953-verifying-an-alien-dictionary/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 09 Dec 2018 09:15:20 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[mapping]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4415</guid>

					<description><![CDATA[<p>Problem In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters. Given a&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-953-verifying-an-alien-dictionary/">花花酱 LeetCode 953. Verifying an Alien Dictionary</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>Problem</h1>
<p>In an alien language, surprisingly they also use english lowercase letters, but possibly in a different <code>order</code>. The <code>order</code> of the alphabet is some permutation of lowercase letters.</p>
<p>Given a sequence of <code>words</code> written in the alien language, and the <code>order</code> of the alphabet, return <code>true</code> if and only if the given <code>words</code> are sorted lexicographicaly in this alien language.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>words = <span id="example-input-1-1">["hello","leetcode"]</span>, order = <span id="example-input-1-2">"hlabcdefgijkmnopqrstuvwxyz"</span>
<strong>Output: </strong><span id="example-output-1">true</span>
<strong>Explanation: </strong><span id="example-output-1">As 'h' comes before 'l' in this language, then the sequence is sorted.</span>
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>words = <span id="example-input-2-1">["word","world","row"]</span>, order = <span id="example-input-2-2">"worldabcefghijkmnpqstuvxyz"</span>
<strong>Output: </strong><span id="example-output-2">false</span>
<strong>Explanation: </strong><span id="example-output-1">As 'd' comes after 'l' in this language, then words[0] &gt; words[1], hence the sequence is unsorted.</span>
</pre>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false "><strong>Input: </strong>words = <span id="example-input-3-1">["apple","app"]</span>, order = <span id="example-input-3-2">"abcdefghijklmnopqrstuvwxyz"</span>
<strong>Output: </strong><span id="example-output-3">false
</span><strong>Explanation: </strong>The first three characters "app" match, and the second string is shorter (in size.) According to lexicographical rules "apple" &gt; "app", because 'l' &gt; '∅', where '∅' is defined as the blank character which is less than any other character (<a href="https://en.wikipedia.org/wiki/Lexicographical_order" target="_blank" rel="noopener">More info</a>).
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= words.length &lt;= 100</code></li>
<li><code>1 &lt;= words[i].length &lt;= 20</code></li>
<li><code>order.length == 26</code></li>
<li>All characters in <code>words[i]</code> and <code>order</code> are english lowercase letters.</li>
</ol>
<h1><strong>Solution: Hashtable</strong></h1>
<p>Time complexity: O(sum(len(words[i])))</p>
<p>Space complexity: O(26)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, 8 ms
class Solution {
public:
  bool isAlienSorted(vector&lt;string&gt;&amp; words, string order) {
    vector&lt;char&gt; m(26);
    for (int i = 0; i &lt; 26; ++i)
      m[order[i] - 'a'] = 'a' + i;
    for (int i = 0; i &lt; words.size(); ++i) {      
      for (int j = 0; j &lt; words[i].length(); ++j)
        words[i][j] = m[words[i][j] - 'a'];      
      if (i &gt; 0 &amp;&amp; words[i] &lt; words[i - 1]) return false;      
    }
    return true;        
  }
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-953-verifying-an-alien-dictionary/">花花酱 LeetCode 953. Verifying an Alien Dictionary</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-953-verifying-an-alien-dictionary/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 768. Max Chunks To Make Sorted II</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-768-max-chunks-to-make-sorted-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-768-max-chunks-to-make-sorted-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 03 Feb 2018 05:17:50 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[mapping]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1745</guid>

					<description><![CDATA[<p>题目大意：给你一堆数让你分块，每块独立排序后和整个数组排序的结果相同，问你最多能分为几块。 Problem: This question is the same as &#8220;Max Chunks to Make Sorted&#8221; except the integers of the given array are not necessarily distinct, the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-768-max-chunks-to-make-sorted-ii/">花花酱 LeetCode 768. Max Chunks To Make Sorted 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>题目大意：给你一堆数让你分块，每块独立排序后和整个数组排序的结果相同，问你最多能分为几块。</p>
<p><strong>Problem:</strong></p>
<p><em>This question is the same as &#8220;Max Chunks to Make Sorted&#8221; except the integers of the given array are not necessarily distinct, the input array could be up to length <code>2000</code>, and the elements could be up to <code>10**8</code>.</em></p>
<hr />
<p>Given an array <code>arr</code> of integers (<strong>not necessarily distinct</strong>), we split the array into some number of &#8220;chunks&#8221; (partitions), and individually sort each chunk.  After concatenating them, the result equals the sorted array.</p>
<p>What is the most number of chunks we could have made?</p>
<p><strong>Example 1:</strong></p><pre class="crayon-plain-tag">Input: arr = [5,4,3,2,1]
Output: 1
Explanation:
Splitting into two or more chunks will not return the required result.
For example, splitting into [5, 4], [3, 2, 1] will result in [4, 5, 1, 2, 3], which isn't sorted.</pre><p><strong>Example 2:</strong></p><pre class="crayon-plain-tag">Input: arr = [2,1,3,4,4]
Output: 4
Explanation:
We can split into two chunks, such as [2, 1], [3, 4, 4].
However, splitting into [2, 1], [3], [4], [4] is the highest number of chunks possible.</pre><p><strong>Note:</strong></p>
<ul>
<li><code>arr</code> will have length in range <code>[1, 2000]</code>.</li>
<li><code>arr[i]</code> will be an integer in range <code>[0, 10**8]</code>.</li>
</ul>
<p><strong>Idea: </strong></p>
<p>Reduce the problem to <a href="http://zxi.mytechroad.com/blog/difficulty/medium/769-max-chunks-to-make-sorted/">花花酱 769. Max Chunks To Make Sorted</a> by creating a mapping from number to index in the sorted array.</p>
<p>arr = [2, 3, 5, 4, 4]</p>
<p>sorted = [2, 3, 4, 4, 5]</p>
<p>indices = [0, 1, 4, 2, 3]</p>
<p><strong>Solution: Mapping</strong></p>
<p>Time complexity: O(nlogn)</p>
<p>Space complexity: O(n)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 19 ms
class Solution {
public:
  int maxChunksToSorted(vector&lt;int&gt;&amp; arr) {
    vector&lt;int&gt; v(arr.size());
    std::iota(v.begin(), v.end(), 0);
    std::sort(v.begin(), v.end(), [&amp;arr](const int i1, const int i2){
      return arr[i1] == arr[i2] ? (i1 &lt; i2) : arr[i1] &lt; arr[i2];
    });
    int ans = 0;
    int m = 0;
    for (int i = 0; i &lt; v.size(); ++i) {
      m = max(m, v[i]);
      if (m == i) ++ans;
    }
    return ans;
  }
};</pre><p>Python3</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 95 ms
"""
class Solution:
  def maxChunksToSorted(self, arr):
    v = sorted([(n &lt;&lt; 32) | i for i, n in enumerate(arr)])    
    m = 0
    ans = 0
    for i, n in enumerate(v):
      m = max(m, n &amp; 0xffffffff)
      if i == m: ans += 1    
    return ans</pre><p>&nbsp;</p>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-768-max-chunks-to-make-sorted-ii/">花花酱 LeetCode 768. Max Chunks To Make Sorted 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/array/leetcode-768-max-chunks-to-make-sorted-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
