<?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>Trie Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/category/trie/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/category/trie/</link>
	<description></description>
	<lastBuildDate>Mon, 28 Dec 2020 08:12:11 +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>Trie Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/category/trie/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1707. Maximum XOR With an Element From Array</title>
		<link>https://zxi.mytechroad.com/blog/trie/leetcode-1707-maximum-xor-with-an-element-from-array/</link>
					<comments>https://zxi.mytechroad.com/blog/trie/leetcode-1707-maximum-xor-with-an-element-from-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 27 Dec 2020 21:54:20 +0000</pubDate>
				<category><![CDATA[Trie]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[sorting]]></category>
		<category><![CDATA[trie]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7871</guid>

					<description><![CDATA[<p>You are given an array&#160;nums&#160;consisting of non-negative integers. You are also given a&#160;queries&#160;array, where&#160;queries[i] = [xi, mi]. The answer to the&#160;ith&#160;query is the maximum bitwise&#160;XOR&#160;value&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/trie/leetcode-1707-maximum-xor-with-an-element-from-array/">花花酱 LeetCode 1707. Maximum XOR With an Element From 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[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1707. Maximum XOR With an Element From Array - 刷题找工作 EP377" width="500" height="281" src="https://www.youtube.com/embed/k0e9tM7gU3E?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>You are given an array&nbsp;<code>nums</code>&nbsp;consisting of non-negative integers. You are also given a&nbsp;<code>queries</code>&nbsp;array, where&nbsp;<code>queries[i] = [x<sub>i</sub>, m<sub>i</sub>]</code>.</p>



<p>The answer to the&nbsp;<code>i<sup>th</sup></code>&nbsp;query is the maximum bitwise&nbsp;<code>XOR</code>&nbsp;value of&nbsp;<code>x<sub>i</sub></code>&nbsp;and any element of&nbsp;<code>nums</code>&nbsp;that does not exceed&nbsp;<code>m<sub>i</sub></code>. In other words, the answer is&nbsp;<code>max(nums[j] XOR x<sub>i</sub>)</code>&nbsp;for all&nbsp;<code>j</code>&nbsp;such that&nbsp;<code>nums[j] &lt;= m<sub>i</sub></code>. If all elements in&nbsp;<code>nums</code>&nbsp;are larger than&nbsp;<code>m<sub>i</sub></code>, then the answer is&nbsp;<code>-1</code>.</p>



<p>Return&nbsp;<em>an integer array&nbsp;</em><code>answer</code><em>&nbsp;where&nbsp;</em><code>answer.length == queries.length</code><em>&nbsp;and&nbsp;</em><code>answer[i]</code><em>&nbsp;is the answer to the&nbsp;</em><code>i<sup>th</sup></code><em>&nbsp;query.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [0,1,2,3,4], queries = [[3,1],[1,3],[5,6]]
<strong>Output:</strong> [3,3,7]
<strong>Explanation:</strong>
1) 0 and 1 are the only two integers not greater than 1. 0 XOR 3 = 3 and 1 XOR 3 = 2. The larger of the two is 3.
2) 1 XOR 2 = 3.
3) 5 XOR 2 = 7.
</pre>



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



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



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



<ul><li><code>1 &lt;= nums.length, queries.length &lt;= 10<sup>5</sup></code></li><li><code>queries[i].length == 2</code></li><li><code>0 &lt;= nums[j], x<sub>i</sub>, m<sub>i</sub>&nbsp;&lt;= 10<sup>9</sup></code></li></ul>



<h2><strong>Solution: Trie on the fly</strong></h2>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1707-ep377-1.png" alt="" class="wp-image-7874" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1707-ep377-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1707-ep377-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1707-ep377-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1707-ep377-2.png" alt="" class="wp-image-7875" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1707-ep377-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1707-ep377-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1707-ep377-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1707-ep377-3.png" alt="" class="wp-image-7876" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1707-ep377-3.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1707-ep377-3-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/1707-ep377-3-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p>Similar idea to <a href="https://zxi.mytechroad.com/blog/trie/leetcode-421-maximum-xor-of-two-numbers-in-an-array/">https://zxi.mytechroad.com/blog/trie/leetcode-421-maximum-xor-of-two-numbers-in-an-array/</a></p>



<p>We can build the trie on the fly by sorting nums in ascending order and queries by its limit, insert nums into the trie up the limit.</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Trie {
public:
  Trie(): children{nullptr, nullptr} {}  
  Trie* children[2];
};
class Solution {
public:
  vector&lt;int&gt; maximizeXor(vector&lt;int&gt;&amp; nums, vector&lt;vector&lt;int&gt;&gt;&amp; queries) {
    const int n = nums.size();
    sort(begin(nums), end(nums));    
    
    const int Q = queries.size();
    for (int i = 0; i &lt; Q; ++i)
      queries[i].push_back(i);
    sort(begin(queries), end(queries), [](const auto&amp; q1, const auto&amp; q2) {
      return q1[1] &lt; q2[1];
    });
    
    Trie root;    
    auto insert = [&amp;](int num) {
      Trie* node = &amp;root; 
      for (int i = 31; i &gt;= 0; --i) {
        int bit = (num &gt;&gt; i) &amp; 1;
        if (!node-&gt;children[bit])
          node-&gt;children[bit] = new Trie();
        node = node-&gt;children[bit];
      }
    };
        
    auto query = [&amp;](int num) {
      Trie* node = &amp;root;
      int sum = 0;
      for (int i = 31; i &gt;= 0; --i) {
        if (!node) return -1;
        int bit = (num &gt;&gt; i) &amp; 1;
        if (node-&gt;children[1 - bit]) {
          sum |= (1 &lt;&lt; i);
          node = node-&gt;children[1 - bit];
        } else {
          node = node-&gt;children[bit];
        }
      }
      return sum;
    };
    
    vector&lt;int&gt; ans(Q);
    int i = 0;
    for (const auto&amp; q : queries) {
      while (i &lt; n &amp;&amp; nums[i] &lt;= q[1]) insert(nums[i++]);
      ans[q[2]] = query(q[0]);
    }  
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/trie/leetcode-1707-maximum-xor-with-an-element-from-array/">花花酱 LeetCode 1707. Maximum XOR With an Element From 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/trie/leetcode-1707-maximum-xor-with-an-element-from-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 421. Maximum XOR of Two Numbers in an Array</title>
		<link>https://zxi.mytechroad.com/blog/trie/leetcode-421-maximum-xor-of-two-numbers-in-an-array/</link>
					<comments>https://zxi.mytechroad.com/blog/trie/leetcode-421-maximum-xor-of-two-numbers-in-an-array/#comments</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 27 Dec 2020 21:31:59 +0000</pubDate>
				<category><![CDATA[Trie]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[trie]]></category>
		<category><![CDATA[xor]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7868</guid>

					<description><![CDATA[<p>Given an integer array&#160;nums, return&#160;the maximum result of&#160;nums[i] XOR nums[j], where&#160;0 ≤ i ≤ j &#60; n. Follow up:&#160;Could you do this in&#160;O(n)&#160;runtime? Example 1:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/trie/leetcode-421-maximum-xor-of-two-numbers-in-an-array/">花花酱 LeetCode 421. Maximum XOR of Two Numbers in an 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 integer array&nbsp;<code>nums</code>, return&nbsp;<em>the maximum result of&nbsp;<code>nums[i] XOR nums[j]</code></em>, where&nbsp;<code>0 ≤ i ≤ j &lt; n</code>.</p>



<p><strong>Follow up:</strong>&nbsp;Could you do this in&nbsp;<code>O(n)</code>&nbsp;runtime?</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.</pre>



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



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



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



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



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [8,10,2]
<strong>Output:</strong> 10
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li><li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup>&nbsp;- 1</code></li></ul>



<h2><strong>Solution: Trie</strong></h2>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Trie {
public:
  Trie(): children(2) {}  
  vector&lt;unique_ptr&lt;Trie&gt;&gt; children;
};
class Solution {
public:
  int findMaximumXOR(vector&lt;int&gt;&amp; nums) {
    Trie root;
    
    // Insert a number into the trie.
    auto insert = [&amp;](Trie* node, int num) {
      for (int i = 31; i &gt;= 0; --i) {
        int bit = (num &gt;&gt; i) &amp; 1;
        if (!node-&gt;children[bit])
          node-&gt;children[bit] = std::make_unique&lt;Trie&gt;();
        node = node-&gt;children[bit].get();
      }
    };
  
    // Find max xor sum of num and another element in the array.
    auto query = [&amp;](Trie* node, int num) {
      int sum = 0;
      for (int i = 31; i &gt;= 0; --i) {
        int bit = (num &gt;&gt; i) &amp; 1;
        if (node-&gt;children[1 - bit]) {
          sum += (1 &lt;&lt; i);
          node = node-&gt;children[1 - bit].get();
        } else {
          node = node-&gt;children[bit].get();
        }
      }
      return sum;
    };
    
    // Insert all numbers.
    for (int x : nums) 
      insert(&amp;root, x);
    
    int ans = 0;
    // For each number find the maximum xor sum.
    for (int x : nums) 
      ans = max(ans, query(&amp;root, x));
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/trie/leetcode-421-maximum-xor-of-two-numbers-in-an-array/">花花酱 LeetCode 421. Maximum XOR of Two Numbers in an 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/trie/leetcode-421-maximum-xor-of-two-numbers-in-an-array/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
