<?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>degrees Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/degrees/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/degrees/</link>
	<description></description>
	<lastBuildDate>Sun, 11 Oct 2020 05:14:36 +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>degrees Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/degrees/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1615. Maximal Network Rank</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-1615-maximal-network-rank/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-1615-maximal-network-rank/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 11 Oct 2020 05:14:11 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[all pairs]]></category>
		<category><![CDATA[degrees]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7477</guid>

					<description><![CDATA[<p>There is an infrastructure of&#160;n&#160;cities with some number of&#160;roads&#160;connecting these cities. Each&#160;roads[i] = [ai, bi]&#160;indicates that there is a bidirectional road between cities&#160;ai&#160;and&#160;bi. The&#160;network rankof&#160;two&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1615-maximal-network-rank/">花花酱 LeetCode 1615. Maximal Network Rank</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>There is an infrastructure of&nbsp;<code>n</code>&nbsp;cities with some number of&nbsp;<code>roads</code>&nbsp;connecting these cities. Each&nbsp;<code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>&nbsp;indicates that there is a bidirectional road between cities&nbsp;<code>a<sub>i</sub></code>&nbsp;and&nbsp;<code>b<sub>i</sub></code>.</p>



<p>The&nbsp;<strong>network rank</strong>of&nbsp;<strong>two different cities</strong>&nbsp;is defined as the total number of&nbsp;<strong>directly</strong>&nbsp;connected roads to&nbsp;<strong>either</strong>&nbsp;city. If a road is directly connected to both cities, it is only counted&nbsp;<strong>once</strong>.</p>



<p>The&nbsp;<strong>maximal network rank&nbsp;</strong>of the infrastructure is the&nbsp;<strong>maximum network rank</strong>&nbsp;of all pairs of different cities.</p>



<p>Given the integer&nbsp;<code>n</code>&nbsp;and the array&nbsp;<code>roads</code>, return&nbsp;<em>the&nbsp;<strong>maximal network rank</strong>&nbsp;of the entire infrastructure</em>.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/09/21/ex1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4, roads = [[0,1],[0,3],[1,2],[1,3]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The network rank of cities 0 and 1 is 4 as there are 4 roads that are connected to either 0 or 1. The road between 0 and 1 is only counted once.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/09/21/ex2.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 5, roads = [[0,1],[0,3],[1,2],[1,3],[2,3],[2,4]]
<strong>Output:</strong> 5
<strong>Explanation:</strong> There are 5 roads that are connected to cities 1 or 2.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 8, roads = [[0,1],[1,2],[2,3],[2,4],[5,6],[5,7]]
<strong>Output:</strong> 5
<strong>Explanation:</strong> The network rank of 2 and 5 is 5. Notice that all the cities do not have to be connected.
</pre>



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



<ul><li><code>2 &lt;= n &lt;= 100</code></li><li><code>0 &lt;= roads.length &lt;= n * (n - 1) / 2</code></li><li><code>roads[i].length == 2</code></li><li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub>&nbsp;&lt;= n-1</code></li><li><code>a<sub>i</sub>&nbsp;!=&nbsp;b<sub>i</sub></code></li><li>Each&nbsp;pair of cities has&nbsp;<strong>at most one</strong>&nbsp;road connecting them.</li></ul>



<h2><strong>Solution: Counting degrees and all pairs</strong></h2>



<p>Counting degrees for each node, if a and b are not connected, ans = degrees(a) + degrees(b), otherwise ans -= 1</p>



<p>Time complexity: O(E + V^2)<br>Space complexity: O(E)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int maximalNetworkRank(int n, vector&lt;vector&lt;int&gt;&gt;&amp; roads) {
    vector&lt;int&gt; degrees(n);
    unordered_set&lt;int&gt; connected;
    for (const auto&amp; road : roads) {
      ++degrees[road[0]];
      ++degrees[road[1]];
      connected.insert((road[0] &lt;&lt; 16) | road[1]);
      connected.insert((road[1] &lt;&lt; 16) | road[0]);
    }
    int ans = 0;
    for (int i = 0; i &lt; n; ++i)
      for (int j = i + 1; j &lt; n; ++j)
        ans = max(ans, degrees[i] + degrees[j] 
                         - (connected.count((i &lt;&lt; 16) | j) ? 1 : 0));
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1615-maximal-network-rank/">花花酱 LeetCode 1615. Maximal Network Rank</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-1615-maximal-network-rank/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
