<?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>freq Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/freq/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/freq/</link>
	<description></description>
	<lastBuildDate>Sun, 11 Sep 2022 14:54: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>freq Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/freq/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2404. Most Frequent Even Element</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-2404-most-frequent-even-element%ef%bf%bc/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-2404-most-frequent-even-element%ef%bf%bc/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 11 Sep 2022 14:54:39 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[freq]]></category>
		<category><![CDATA[hashtable]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9804</guid>

					<description><![CDATA[<p>Given an integer array&#160;nums, return&#160;the most frequent even element. If there is a tie, return the&#160;smallest&#160;one. If there is no such element, return&#160;-1. Example 1:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2404-most-frequent-even-element%ef%bf%bc/">花花酱 LeetCode 2404. Most Frequent Even Element</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 most frequent even element</em>.</p>



<p>If there is a tie, return the&nbsp;<strong>smallest</strong>&nbsp;one. If there is no such element, return&nbsp;<code>-1</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [0,1,2,2,4,4,1]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The even elements are 0, 2, and 4. Of these, 2 and 4 appear the most.
We return the smallest one, which is 2.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [4,4,4,9,2,4]
<strong>Output:</strong> 4
<strong>Explanation:</strong> 4 is the even element appears the most.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [29,47,21,41,13,37,25,7]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no even element.
</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 2000</code></li><li><code>0 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li></ul>



<h2><strong>Solution: Hashtable</strong></h2>



<p>Use a hashtable to store the frequency of even numbers.</p>



<p>Time complexity: O(n)<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 Solution {
public:
  int mostFrequentEven(vector&lt;int&gt;&amp; nums) {
    unordered_map&lt;int, int&gt; m;
    int ans = -1;
    int best = 0;
    for (int x : nums) {
      if (x &amp; 1) continue;
      int cur = ++m[x];
      if (cur &gt; best) {
        best = cur;
        ans = x;
      } else if (cur == best &amp;&amp; x &lt; ans) {
        ans = x;
      }
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2404-most-frequent-even-element%ef%bf%bc/">花花酱 LeetCode 2404. Most Frequent Even Element</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-2404-most-frequent-even-element%ef%bf%bc/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1394. Find Lucky Integer in an Array</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1394-find-lucky-integer-in-an-array/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1394-find-lucky-integer-in-an-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 29 Mar 2020 04:48:39 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[freq]]></category>
		<category><![CDATA[hashtable]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6546</guid>

					<description><![CDATA[<p>Given an array of integers&#160;arr, a lucky integer is an integer which has a frequency in the array equal to its value. Return&#160;a lucky integer&#160;in&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1394-find-lucky-integer-in-an-array/">花花酱 LeetCode 1394. Find Lucky Integer 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 array of integers&nbsp;<code>arr</code>, a lucky integer is an integer which has a frequency in the array equal to its value.</p>



<p>Return&nbsp;<em>a lucky integer</em>&nbsp;in the array. If there are multiple lucky integers return the&nbsp;<strong>largest</strong>&nbsp;of them. If there is no lucky&nbsp;integer return&nbsp;<strong>-1</strong>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [2,2,3,4]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only lucky number in the array is 2 because frequency[2] == 2.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [1,2,2,3,3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 1, 2 and 3 are all lucky numbers, return the largest of them.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [2,2,2,3,3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There are no lucky numbers in the array.
</pre>



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



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



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



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



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



<ul><li><code>1 &lt;= arr.length &lt;= 500</code></li><li><code>1 &lt;= arr[i] &lt;= 500</code></li></ul>



<h2><strong>Solution: Hashtable</strong></h2>



<p>Time complexity: O(n)<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 Solution {
public:
  int findLucky(vector&lt;int&gt;&amp; arr) {
    unordered_map&lt;int, int&gt; freq;
    for (int x : arr) ++freq[x];
    int ans = -1;
    for (const auto&amp; [key, count] : freq)
      if (key == count) ans = max(ans, key);
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1394-find-lucky-integer-in-an-array/">花花酱 LeetCode 1394. Find Lucky Integer 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/hashtable/leetcode-1394-find-lucky-integer-in-an-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 895. Maximum Frequency Stack</title>
		<link>https://zxi.mytechroad.com/blog/desgin/leetcode-895-maximum-frequency-stack/</link>
					<comments>https://zxi.mytechroad.com/blog/desgin/leetcode-895-maximum-frequency-stack/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 26 Aug 2018 16:14:01 +0000</pubDate>
				<category><![CDATA[Desgin]]></category>
		<category><![CDATA[freq]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[priority queue]]></category>
		<category><![CDATA[stack]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3705</guid>

					<description><![CDATA[<p>Problem Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack has two functions: push(int x), which pushes an integer x onto the stack. pop(),&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/desgin/leetcode-895-maximum-frequency-stack/">花花酱 LeetCode 895. Maximum Frequency Stack</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/IkrGghj6_fk?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>Implement <code>FreqStack</code>, a class which simulates the operation of a stack-like data structure.</p>
<p><code>FreqStack</code> has two functions:</p>
<ul>
<li><code>push(int x)</code>, which pushes an integer <code>x</code> onto the stack.</li>
<li><code>pop()</code>, which <strong>removes</strong> and returns the most frequent element in the stack.
<ul>
<li>If there is a tie for most frequent element, the element closest to the top of the stack is removed and returned.</li>
</ul>
</li>
</ul>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false "><strong>Input: </strong>
<span id="example-input-1-1">["FreqStack","push","push","push","push","push","push","pop","pop","pop","pop"]</span>,
<span id="example-input-1-2">[[],[5],[7],[5],[7],[4],[5],[],[],[],[]]</span>
<strong>Output: </strong><span id="example-output-1">[null,null,null,null,null,null,null,5,7,5,4]</span>
<strong>Explanation</strong>:
After making six .push operations, the stack is [5,7,5,7,4,5] from bottom to top.  Then:

pop() -&gt; returns 5, as 5 is the most frequent.
The stack becomes [5,7,5,7,4].

pop() -&gt; returns 7, as 5 and 7 is the most frequent, but 7 is closest to the top.
The stack becomes [5,7,5,4].

pop() -&gt; returns 5.
The stack becomes [5,7,4].

pop() -&gt; returns 4.
The stack becomes [5,7].
</pre>
<p><strong>Note:</strong></p>
<ul>
<li>Calls to <code>FreqStack.push(int x)</code> will be such that <code>0 &lt;= x &lt;= 10^9</code>.</li>
<li>It is guaranteed that <code>FreqStack.pop()</code> won&#8217;t be called if the stack has zero elements.</li>
<li>The total number of <code>FreqStack.push</code> calls will not exceed <code>10000</code> in a single test case.</li>
<li>The total number of <code>FreqStack.pop</code> calls will not exceed <code>10000</code> in a single test case.</li>
<li>The total number of <code>FreqStack.push</code> and <code>FreqStack.pop</code> calls will not exceed <code>150000</code> across all test cases.</li>
</ul>
<p><ins class="adsbygoogle" style="display: block; text-align: center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-2404451723245401" data-ad-slot="7983117522"> </ins></p>
<h1><img class="alignnone size-full wp-image-3710" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/895-ep220.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/895-ep220.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/895-ep220-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/895-ep220-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></h1>
<h1><strong>Solution 1: Buckets</strong></h1>
<p>We have n  stacks. The i-th stack has the of elements with freq i when pushed.</p>
<p>We keep tracking the freq of each element.</p>
<p>push(x): stacks[++freq(x)].push(x)  # inc x&#8217;s freq and push it onto freq-th stack</p>
<p>pop(): x = stacks[max_freq].pop(), &#8211;freq(x); # pop element x from the max_freq stack and dec it&#8217;s freq.</p>
<p>Time complexity: O(1) push / pop</p>
<p>Space complexity: O(n)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 144 ms
class FreqStack {
public:
  FreqStack() {}

  void push(int x) {
    auto it = freq_.find(x);
    if (it == freq_.end())
      it = freq_.emplace(x, 0).first;    
    int freq = ++it-&gt;second;    
    if (stacks_.size() &lt; freq) stacks_.push_back({});
    stacks_[freq - 1].push(x);
  }

  int pop() {    
    int x = stacks_.back().top();    
    stacks_.back().pop();
    if (stacks_.back().empty())
      stacks_.pop_back();
    auto it = freq_.find(x);
    if (!(--it-&gt;second))
      freq_.erase(it);
    return x;
  }
private:  
  vector&lt;stack&lt;int&gt;&gt; stacks_;
  unordered_map&lt;int, int&gt; freq_;
};</pre><p></div></div></p>
<h1>Solution2: Priority Queue</h1>
<p>Use a max heap with key: (freq, seq), the max freq and closest to the top of stack element will be extracted first.</p>
<p>Time complexity: O(logn)</p>
<p>Space complexity: O(n)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 132 ms
class FreqStack {
public:
  FreqStack() {}

  void push(int x) {    
    int key = (++f_[x] &lt;&lt; 16) | (++seq_);
    q_.emplace(key, x);
  }

  int pop() {
    int x = q_.top().second; q_.pop();    
    if (--f_[x] == 0)
      f_.erase(x);
    return x;
  }
private:
  int seq_ = 0;
  unordered_map&lt;int, int&gt; f_; // {x -&gt; freq}
  priority_queue&lt;pair&lt;int, int&gt;&gt; q_; // {freq | seq_, x}
};</pre><p></div></div></p>
<h1><strong>Related Problems</strong></h1>
<ul>
<li><a href="https://zxi.mytechroad.com/blog/hashtable/leetcode-460-lfu-cache/">花花酱 LeetCode 460. LFU Cache</a></li>
<li><a href="https://zxi.mytechroad.com/blog/hashtable/leetcode-146-lru-cache/">花花酱 LeetCode 146. LRU Cache O(1)</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/desgin/leetcode-895-maximum-frequency-stack/">花花酱 LeetCode 895. Maximum Frequency Stack</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/desgin/leetcode-895-maximum-frequency-stack/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
