<?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>smulation Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/smulation/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/smulation/</link>
	<description></description>
	<lastBuildDate>Tue, 14 Apr 2020 00:28: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>smulation Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/smulation/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1409. Queries on a Permutation With Key</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-1409-queries-on-a-permutation-with-key/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-1409-queries-on-a-permutation-with-key/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 12 Apr 2020 06:02:52 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[fenwick tree]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[smulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6597</guid>

					<description><![CDATA[<p>Given the array&#160;queries&#160;of positive integers between&#160;1&#160;and&#160;m, you have to process all&#160;queries[i]&#160;(from&#160;i=0&#160;to&#160;i=queries.length-1) according to the following rules: In the beginning, you have the permutation&#160;P=[1,2,3,...,m]. For the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1409-queries-on-a-permutation-with-key/">花花酱 LeetCode 1409. Queries on a Permutation With Key</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-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1409. Queries on a Permutation With Key - 刷题找工作 EP319" width="500" height="375" src="https://www.youtube.com/embed/DwtijVbS3G0?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given the array&nbsp;<code>queries</code>&nbsp;of positive integers between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>m</code>, you have to process all&nbsp;<code>queries[i]</code>&nbsp;(from&nbsp;<code>i=0</code>&nbsp;to&nbsp;<code>i=queries.length-1</code>) according to the following rules:</p>



<ul><li>In the beginning, you have the permutation&nbsp;<code>P=[1,2,3,...,m]</code>.</li><li>For the current&nbsp;<code>i</code>, find the position of&nbsp;<code>queries[i]</code>&nbsp;in the permutation&nbsp;<code>P</code>&nbsp;(<strong>indexing from 0</strong>) and then move this at the beginning of the permutation&nbsp;<code>P.</code>&nbsp;Notice that the position of&nbsp;<code>queries[i]</code>&nbsp;in&nbsp;<code>P</code>&nbsp;is the result for&nbsp;<code>queries[i]</code>.</li></ul>



<p>Return an array containing the result for the given&nbsp;<code>queries</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> queries = [3,1,2,1], m = 5
<strong>Output:</strong> [2,1,2,1] 
<strong>Explanation:</strong> The queries are processed as follow: 
For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is <strong>2</strong>, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. 
For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is <strong>1</strong>, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. 
For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is <strong>2</strong>, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. 
For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is <strong>1</strong>, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. 
Therefore, the array containing the result is [2,1,2,1].  
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> queries = [4,1,2,2], m = 4
<strong>Output:</strong> [3,1,2,0]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> queries = [7,5,5,8,3], m = 8
<strong>Output:</strong> [6,5,0,7,5]
</pre>



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



<ul><li><code>1 &lt;= m &lt;= 10^3</code></li><li><code>1 &lt;= queries.length &lt;= m</code></li><li><code>1 &lt;= queries[i] &lt;= m</code></li></ul>



<h2><strong>Solution1: Simulation + Hashtable</strong></h2>



<p>Use a hashtable to store the location of each key.<br>For each query q, use h[q] to get the index of q, for each key, if its current index is less than q, increase their indices by 1. (move right). Set h[q] to 0.</p>



<p>Time complexity: O(q*m)<br>Space complexity: O(m)</p>



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



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

<pre class="crayon-plain-tag">class Solution {
public:
  vector&lt;int&gt; processQueries(vector&lt;int&gt;&amp; queries, int m) {
    vector&lt;int&gt; p(m + 1);
    iota(begin(p), end(p), -1);
    vector&lt;int&gt; ans;
    for (int q : queries) {      
      ans.push_back(p[q]);
      for (int i = 1; i &lt;= m; ++i)
        if (p[i] &lt; p[q]) ++p[i];
      p[q] = 0;
    }      
    return ans;
  }
};</pre>
</div></div>



<h2><strong>Solution 2: Fenwick Tree + HashTable</strong></h2>



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



<p>Time complexity: O(qlogm)<br>Space complexity: O(m)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Fenwick {
public:
  explicit Fenwick(int n): vals_(n) {}
  
  // sum(A[1~i])
  int query(int i) const {
    int s = 0;
    while (i &gt; 0) {
      s += vals_[i];
      i -= i &amp; -i;
    }
    return s;
  }
  
  // A[i] += delta
  void update(int i, int delta) {
    while (i &lt; vals_.size()) {
      vals_[i] += delta;
      i += i &amp; -i;
    }
  }
private:
  vector&lt;int&gt; vals_;
};

class Solution {
public:
  vector&lt;int&gt; processQueries(vector&lt;int&gt;&amp; queries, int m) {
    Fenwick tree(m * 2  + 1);
    vector&lt;int&gt; pos(m + 1);
    for (int i = 1; i &lt;= m; ++i)     
      tree.update(pos[i] = i + m, 1);
    
    vector&lt;int&gt; ans;
    for (int q : queries) {
      ans.push_back(tree.query(pos[q] - 1));
      tree.update(pos[q], -1); // set to 0.      
      tree.update(pos[q] = m--, 1); // move to the front.
    }
    return ans;
  }
};</pre>
</div></div>



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

<pre class="crayon-plain-tag">class Fenwick:
  def __init__(self, n):
    self.n = n
    self.val = [0] * n
  
  def query(self, i):
    s = 0
    while i &gt; 0:
      s += self.val[i]
      i -= i &amp; -i
    return s
  
  def update(self, i, delta):
    while i &lt; self.n:
      self.val[i] += delta
      i += i &amp; -i
    
class Solution:
  def processQueries(self, queries: List[int], m: int) -&gt; List[int]:    
    pos = [i + m for i in range(m + 1)]
    tree = Fenwick(2 * m + 1)
    for i in range(1, m + 1): tree.update(i + m, 1)    
    ans = []
    for q in queries:
      ans.append(tree.query(pos[q] - 1))
      tree.update(pos[q], -1)
      pos[q] = m
      m -= 1
      tree.update(pos[q], 1)
    return ans</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1409-queries-on-a-permutation-with-key/">花花酱 LeetCode 1409. Queries on a Permutation With Key</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-1409-queries-on-a-permutation-with-key/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
