<?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>mode Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/mode/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/mode/</link>
	<description></description>
	<lastBuildDate>Fri, 07 Feb 2020 05:38:16 +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>mode Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/mode/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1093. Statistics from a Large Sample</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1093-statistics-from-a-large-sample/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1093-statistics-from-a-large-sample/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 07 Feb 2020 05:38:00 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[median]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[mode]]></category>
		<category><![CDATA[statistics]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6266</guid>

					<description><![CDATA[<p>We sampled integers between&#160;0&#160;and&#160;255, and stored the results in an array&#160;count:&#160;&#160;count[k]&#160;is the number of integers we sampled equal to&#160;k. Return the minimum, maximum, mean, median,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1093-statistics-from-a-large-sample/">花花酱 LeetCode 1093. Statistics from a Large Sample</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>We sampled integers between&nbsp;<code>0</code>&nbsp;and&nbsp;<code>255</code>, and stored the results in an array&nbsp;<code>count</code>:&nbsp;&nbsp;<code>count[k]</code>&nbsp;is the number of integers we sampled equal to&nbsp;<code>k</code>.</p>



<p>Return the minimum, maximum, mean, median, and mode of the sample respectively, as an array of&nbsp;<strong>floating point numbers</strong>.&nbsp; The mode is guaranteed to be unique.</p>



<p><em>(Recall that the median of a sample is:</em></p>



<ul><li><em>The middle element, if the elements of the sample were sorted and the number of elements is odd;</em></li><li><em>The average of the middle two elements, if the elements of the sample were sorted and the number of elements is even.)</em></li></ul>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> count = [0,1,3,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
<strong>Output:</strong> [1.00000,3.00000,2.37500,2.50000,3.00000]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> count = [0,4,3,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
<strong>Output:</strong> [1.00000,4.00000,2.18182,2.00000,1.00000]
</pre>



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



<ol><li><code>count.length == 256</code></li><li><code>1 &lt;= sum(count) &lt;= 10^9</code></li><li>The mode of the sample that count represents is unique.</li><li>Answers within&nbsp;<code>10^-5</code>&nbsp;of the true value will be accepted as correct.</li></ol>



<h2><strong>Solution: TreeMap</strong></h2>



<p>Time complexity: O(1)<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
class Solution {
public:
  vector&lt;double&gt; sampleStats(vector&lt;int&gt;&amp; count) {
    int counts = 0;
    int minVal = 255;
    int maxVal = 0;
    int mode = -1;
    int modeCount = 0;
    long sum = 0;
    map&lt;int, int&gt; m;
    for (int i = 0; i &lt;= 255; ++i) {      
      if (!count[i]) continue;
      sum += static_cast&lt;long&gt;(i) * count[i];
      minVal = min(minVal, i);
      maxVal = max(maxVal, i);
      if (count[i] &gt; modeCount) {
        mode = i;
        modeCount = count[i];
      }
      m[counts += count[i]] = i;
    }
    auto it1 = m.lower_bound(counts / 2);
    double median = it1-&gt;second;    
    auto it2 = next(it1);
    if (counts % 2 == 0 &amp;&amp; it2 != m.end() &amp;&amp; it1-&gt;first == counts / 2) {  
      median = (median + it2-&gt;second) / 2;
    }
    return {minVal, maxVal, static_cast&lt;double&gt;(sum) / counts, median, mode};
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1093-statistics-from-a-large-sample/">花花酱 LeetCode 1093. Statistics from a Large Sample</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-1093-statistics-from-a-large-sample/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 501. Find Mode in Binary Search Tree</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-501-find-mode-in-binary-search-tree/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-501-find-mode-in-binary-search-tree/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 19 Jul 2018 07:53:28 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[mode]]></category>
		<category><![CDATA[O(1)]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3217</guid>

					<description><![CDATA[<p>Problem Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-501-find-mode-in-binary-search-tree/">花花酱 LeetCode 501. Find Mode in Binary Search Tree</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><strong>Problem</strong></h1>
<p>Given a binary search tree (BST) with duplicates, find all the <a href="https://en.wikipedia.org/wiki/Mode_(statistics)" target="_blank" rel="noopener">mode(s)</a> (the most frequently occurred element) in the given BST.</p>
<p>Assume a BST is defined as follows:</p>
<ul>
<li>The left subtree of a node contains only nodes with keys <b>less than or equal to</b> the node&#8217;s key.</li>
<li>The right subtree of a node contains only nodes with keys <b>greater than or equal to</b> the node&#8217;s key.</li>
<li>Both the left and right subtrees must also be binary search trees.</li>
</ul>
<p>&nbsp;</p>
<p>For example:<br />
Given BST <code>[1,null,2,2]</code>,</p><pre class="crayon-plain-tag">1
    \
     2
    /
   2</pre><p>return <code>[2]</code>.</p>
<p><b>Note:</b> If a tree has more than one mode, you can return them in any order.</p>
<p><b>Follow up:</b> Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).</p>
<h1>Solution1: Recursion w/ extra space</h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 8 ms
class Solution {
public:
  vector&lt;int&gt; findMode(TreeNode* root) {            
    inorder(root);    
    return ans_;
  }
private:
  int val_ = 0;
  int count_ = 0;  
  int max_count_ = 0;
  vector&lt;int&gt; ans_;
  
  void inorder(TreeNode* root) {
    if (root == nullptr) return;
    inorder(root-&gt;left);
    visit(root-&gt;val);
    inorder(root-&gt;right);
  }
  
  void visit(int val) {
    if (count_ &gt; 0 &amp;&amp; val == val_) {
      ++count_;   
    } else {
      val_ = val;
      count_ = 1;
    }
    
    if (count_ &gt; max_count_) {
      max_count_ = count_;
      ans_.clear();
    }
    
    if (count_ == max_count_)
      ans_.push_back(val);
  }
};</pre><p>&nbsp;</p>
<h1><strong>Solution2: Recursion w/o extra space</strong></h1>
<p>Two passes. First pass to find the count of the mode, second pass to collect all the modes.</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 8 ms
class Solution {
public:
  vector&lt;int&gt; findMode(TreeNode* root) {            
    inorder(root);    
    mode_count_ = max_count_;    
    count_ = 0;
    inorder(root);
    return ans_;
  }
private:
  int val_ = 0;
  int count_ = 0;
  int mode_count_ = INT_MAX;
  int max_count_ = 0;
  vector&lt;int&gt; ans_;
  
  void inorder(TreeNode* root) {
    if (root == nullptr) return;
    inorder(root-&gt;left);
    visit(root-&gt;val);
    inorder(root-&gt;right);
  }
  
  void visit(int val) {
    if (count_ &gt; 0 &amp;&amp; val == val_) {
      ++count_;      
    } else {
      val_ = val;
      count_ = 1;
    }
    
    if (count_ &gt; max_count_)
      max_count_ = count_;
    
    if (count_ == mode_count_)
      ans_.push_back(val);
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-501-find-mode-in-binary-search-tree/">花花酱 LeetCode 501. Find Mode in Binary Search Tree</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/tree/leetcode-501-find-mode-in-binary-search-tree/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
