<?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>equation Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/equation/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/equation/</link>
	<description></description>
	<lastBuildDate>Thu, 30 Jan 2020 04:22:17 +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>equation Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/equation/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 990. Satisfiability of Equality Equations</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-990-satisfiability-of-equality-equations/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-990-satisfiability-of-equality-equations/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 10 Feb 2019 10:21:32 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[equation]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[union find]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4824</guid>

					<description><![CDATA[<p>Given an array&#160;equations&#160;of strings that represent relationships between variables, each string&#160;equations[i]&#160;has length&#160;4&#160;and takes one of two different forms:&#160;"a==b"&#160;or&#160;"a!=b".&#160; Here,&#160;a&#160;and&#160;b&#160;are lowercase letters (not necessarily different) that&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-990-satisfiability-of-equality-equations/">花花酱 LeetCode 990. Satisfiability of Equality Equations</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 Weekly Contest 123 (989, 990, 991, 992) - 刷题找工作" width="500" height="375" src="https://www.youtube.com/embed/FZPtxuxArLU?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given an array&nbsp;equations&nbsp;of strings that represent relationships between variables, each string&nbsp;<code>equations[i]</code>&nbsp;has length&nbsp;<code>4</code>&nbsp;and takes one of two different forms:&nbsp;<code>"a==b"</code>&nbsp;or&nbsp;<code>"a!=b"</code>.&nbsp; Here,&nbsp;<code>a</code>&nbsp;and&nbsp;<code>b</code>&nbsp;are lowercase letters (not necessarily different) that represent one-letter variable names.</p>



<p>Return&nbsp;<code>true</code>&nbsp;if and only if it is possible to assign integers to variable names&nbsp;so as to satisfy all the given equations.</p>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>["a==b","b!=a"]
<strong>Output: </strong>false
<strong>Explanation: </strong>If we assign say, a = 1 and b = 1, then the first equation is satisfied, but not the second.  There is no way to assign the variables to satisfy both equations.
</pre>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>["b==a","a==b"]
<strong>Output: </strong>true
<strong>Explanation: </strong>We could assign a = 1 and b = 1 to satisfy both equations.
</pre>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>["a==b","b==c","a==c"]
<strong>Output: </strong>true
</pre>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>["a==b","b!=c","c==a"]
<strong>Output: </strong>false
</pre>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>["c==c","b==d","x!=z"]
<strong>Output: </strong>true
</pre>



<p><strong>Note:</strong></p>



<ol><li><code>1 &lt;= equations.length &lt;= 500</code></li><li><code>equations[i].length == 4</code></li><li><code>equations[i][0]</code>&nbsp;and&nbsp;<code>equations[i][3]</code>&nbsp;are lowercase letters</li><li><code>equations[i][1]</code>&nbsp;is either&nbsp;<code>'='</code>&nbsp;or&nbsp;<code>'!'</code></li><li><code>equations[i][2]</code>&nbsp;is&nbsp;<code>'='</code></li></ol>



<h2><strong>Solution: Union Find</strong></h2>



<p>Time complexity: O(n)<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, running time: 12 ms, 7.1 MB
class Solution {
public:
  bool equationsPossible(vector&lt;string&gt;&amp; equations) {        
    iota(begin(parents_), end(parents_), 0);    
    for (const auto&amp; eq : equations)     
      if (eq[1] == '=')
        parents_[find(eq[0])] = find(eq[3]);
    for (const auto&amp; eq : equations)     
      if (eq[1] == '!' &amp;&amp; find(eq[0]) == find(eq[3]))        
          return false;
    return true;
  }
private:
  array&lt;int, 128&gt; parents_;
  int find(int x) {
    if (x != parents_[x])
      parents_[x] = find(parents_[x]);
    return parents_[x];
  }  
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-990-satisfiability-of-equality-equations/">花花酱 LeetCode 990. Satisfiability of Equality Equations</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/graph/leetcode-990-satisfiability-of-equality-equations/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
