<?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>hasthable Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/hasthable/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/hasthable/</link>
	<description></description>
	<lastBuildDate>Sun, 28 Nov 2021 22:20:41 +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>hasthable Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/hasthable/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 166. Fraction to Recurring Decimal</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-166-fraction-to-recurring-decimal/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-166-fraction-to-recurring-decimal/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 28 Nov 2021 22:20:23 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[hasthable]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8877</guid>

					<description><![CDATA[<p>Given two integers representing the&#160;numerator&#160;and&#160;denominator&#160;of a fraction, return&#160;the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. If&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-166-fraction-to-recurring-decimal/">花花酱 LeetCode 166. Fraction to Recurring Decimal</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 two integers representing the&nbsp;<code>numerator</code>&nbsp;and&nbsp;<code>denominator</code>&nbsp;of a fraction, return&nbsp;<em>the fraction in string format</em>.</p>



<p>If the fractional part is repeating, enclose the repeating part in parentheses.</p>



<p>If multiple answers are possible, return&nbsp;<strong>any of them</strong>.</p>



<p>It is&nbsp;<strong>guaranteed</strong>&nbsp;that the length of the answer string is less than&nbsp;<code>10<sup>4</sup></code>&nbsp;for all the given inputs.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> numerator = 1, denominator = 2
<strong>Output:</strong> "0.5"
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> numerator = 2, denominator = 1
<strong>Output:</strong> "2"
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> numerator = 2, denominator = 3
<strong>Output:</strong> "0.(6)"
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> numerator = 4, denominator = 333
<strong>Output:</strong> "0.(012)"
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> numerator = 1, denominator = 5
<strong>Output:</strong> "0.2"
</pre>



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



<ul><li><code>-2<sup>31</sup>&nbsp;&lt;=&nbsp;numerator, denominator &lt;= 2<sup>31</sup>&nbsp;- 1</code></li><li><code>denominator != 0</code></li></ul>



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



<p>Time complexity: O(?)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  string fractionToDecimal(int numerator, int denominator) {
    stringstream ss,ss2;

    long long n = numerator;
    long long d = denominator;

    if ( n &gt; 0 &amp;&amp; d &lt; 0 || n &lt; 0 &amp;&amp; d &gt; 0) {
      ss &lt;&lt; &quot;-&quot;;
      n = abs(n);
      d = abs(d);
    }

    ss &lt;&lt; (n / d);

    long long r = n % d;

    bool loop = false;
    int count = 0;
    int loop_start;

    if (r) {   
      n = r;
      ss &lt;&lt; &quot;.&quot;;
      unordered_map&lt;int, int&gt; rs;
      rs[r] = 0;

      while (r) {
        n = n*10;
        r = n % d;
        ss2 &lt;&lt; (n / d);
        n = r;
        if (r &amp;&amp; rs.count(r)) {
          loop = true;
          loop_start = rs[r];
          break;
        }
        rs[r] = ++count;
      }

      if (loop) {
        auto s2 = ss2.str();
        ss &lt;&lt; s2.substr(0, loop_start) &lt;&lt; &quot;(&quot; &lt;&lt; s2.substr(loop_start) &lt;&lt; &quot;)&quot;;
      } else  {
        ss &lt;&lt; ss2.str();
      }
    }

    return ss.str();
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-166-fraction-to-recurring-decimal/">花花酱 LeetCode 166. Fraction to Recurring Decimal</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-166-fraction-to-recurring-decimal/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1583. Count Unhappy Friends</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1583-count-unhappy-friends/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1583-count-unhappy-friends/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 13 Sep 2020 05:46:42 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[hasthable]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7364</guid>

					<description><![CDATA[<p>You are given a list of&#160;preferences&#160;for&#160;n&#160;friends, where&#160;n&#160;is always&#160;even. For each person&#160;i,&#160;preferences[i]&#160;contains&#160;a list of friends&#160;sorted&#160;in the&#160;order of preference. In other words, a friend earlier in the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1583-count-unhappy-friends/">花花酱 LeetCode 1583. Count Unhappy Friends</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>You are given a list of&nbsp;<code>preferences</code>&nbsp;for&nbsp;<code>n</code>&nbsp;friends, where&nbsp;<code>n</code>&nbsp;is always&nbsp;<strong>even</strong>.</p>



<p>For each person&nbsp;<code>i</code>,&nbsp;<code>preferences[i]</code>&nbsp;contains&nbsp;a list of friends&nbsp;<strong>sorted</strong>&nbsp;in the&nbsp;<strong>order of preference</strong>. In other words, a friend earlier in the list is more preferred than a friend later in the list.&nbsp;Friends in&nbsp;each list are&nbsp;denoted by integers from&nbsp;<code>0</code>&nbsp;to&nbsp;<code>n-1</code>.</p>



<p>All the friends are divided into pairs.&nbsp;The pairings are&nbsp;given in a list&nbsp;<code>pairs</code>,&nbsp;where&nbsp;<code>pairs[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>&nbsp;denotes&nbsp;<code>x<sub>i</sub></code>&nbsp;is paired with&nbsp;<code>y<sub>i</sub></code>&nbsp;and&nbsp;<code>y<sub>i</sub></code>&nbsp;is paired with&nbsp;<code>x<sub>i</sub></code>.</p>



<p>However, this pairing may cause some of the friends to be unhappy.&nbsp;A friend&nbsp;<code>x</code>&nbsp;is unhappy if&nbsp;<code>x</code>&nbsp;is paired with&nbsp;<code>y</code>&nbsp;and there exists a friend&nbsp;<code>u</code>&nbsp;who&nbsp;is paired with&nbsp;<code>v</code>&nbsp;but:</p>



<ul><li><code>x</code>&nbsp;prefers&nbsp;<code>u</code>&nbsp;over&nbsp;<code>y</code>,&nbsp;and</li><li><code>u</code>&nbsp;prefers&nbsp;<code>x</code>&nbsp;over&nbsp;<code>v</code>.</li></ul>



<p>Return&nbsp;<em>the number of unhappy friends</em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
Friend 1 is unhappy because:
- 1 is paired with 0 but prefers 3 over 0, and
- 3 prefers 1 over 2.
Friend 3 is unhappy because:
- 3 is paired with 2 but prefers 1 over 2, and
- 1 prefers 3 over 0.
Friends 0 and 2 are happy.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 2, preferences = [[1], [0]], pairs = [[1, 0]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> Both friends 0 and 1 are happy.
</pre>



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



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



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



<ul><li><code>2 &lt;= n &lt;= 500</code></li><li><code>n</code>&nbsp;is even.</li><li><code>preferences.length&nbsp;== n</code></li><li><code>preferences[i].length&nbsp;== n - 1</code></li><li><code>0 &lt;= preferences[i][j] &lt;= n - 1</code></li><li><code>preferences[i]</code>&nbsp;does not contain&nbsp;<code>i</code>.</li><li>All values in&nbsp;<code>preferences[i]</code>&nbsp;are unique.</li><li><code>pairs.length&nbsp;== n/2</code></li><li><code>pairs[i].length&nbsp;== 2</code></li><li><code>x<sub>i</sub>&nbsp;!= y<sub>i</sub></code></li><li><code>0 &lt;= x<sub>i</sub>, y<sub>i</sub>&nbsp;&lt;= n - 1</code></li><li>Each person is contained in&nbsp;<strong>exactly one</strong>&nbsp;pair.</li></ul>



<h2><strong>Solution: HashTable</strong></h2>



<p>Put the order in a map {x -&gt; {y, order}}, since this is dense, we use can 2D array instead of hasthable which is much faster.</p>



<p>Then for each pair, we just need to check every other pair and compare their orders.</p>



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



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

<pre class="crayon-plain-tag">class Solution {
public:
  int unhappyFriends(int n, vector&lt;vector&lt;int&gt;&gt;&amp; preferences, vector&lt;vector&lt;int&gt;&gt;&amp; pairs) {
    vector&lt;int&gt; p(n);
    for (const auto&amp; pair : pairs) {
      p[pair[0]] = pair[1];
      p[pair[1]] = pair[0];
    }
    vector&lt;vector&lt;int&gt;&gt; orders(n, vector&lt;int&gt;(n));
    for (int x = 0; x &lt; n; ++x)
      for (int i = 0; i &lt; preferences[x].size(); ++i)
        orders[x][preferences[x][i]] = i;
    int ans = 0;
    for (int x = 0; x &lt; n; ++x) {
      const int y = p[x];      
      bool found = false;      
      for (int u = 0; u &lt; n &amp;&amp; !found; ++u) {
        if (u == x || u == y) continue;
        const int v = p[u];
        found |= orders[x][u] &lt; orders[x][y] &amp;&amp; orders[u][x] &lt; orders[u][v]; 
      }
      if (found) ++ans;
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1583-count-unhappy-friends/">花花酱 LeetCode 1583. Count Unhappy Friends</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-1583-count-unhappy-friends/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
