<?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>degree Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/degree/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/degree/</link>
	<description></description>
	<lastBuildDate>Sun, 24 Feb 2019 07:05:48 +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>degree Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/degree/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 997. Find the Town Judge</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-997-find-the-town-judge/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-997-find-the-town-judge/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 24 Feb 2019 07:05:15 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[degree]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[graph]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4885</guid>

					<description><![CDATA[<p>In a town, there are&#160;N&#160;people labelled from&#160;1&#160;to&#160;N.&#160; There is a rumor that one of these people is secretly the town judge. If the&#160;town judge exists,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-997-find-the-town-judge/">花花酱 LeetCode 997. Find the Town Judge</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>In a town, there are&nbsp;<code>N</code>&nbsp;people labelled from&nbsp;<code>1</code>&nbsp;to&nbsp;<code>N</code>.&nbsp; There is a rumor that one of these people is secretly the town judge.</p>



<p>If the&nbsp;town judge exists, then:</p>



<ol><li>The town judge trusts nobody.</li><li>Everybody (except for the town judge) trusts the town judge.</li><li>There is exactly one person that satisfies properties 1 and 2.</li></ol>



<p>You are given&nbsp;<code>trust</code>, an array of pairs&nbsp;<code>trust[i] = [a, b]</code>&nbsp;representing that the person labelled&nbsp;<code>a</code>&nbsp;trusts the person labelled&nbsp;<code>b</code>.</p>



<p>If the town judge exists and can be identified, return the label of the town judge.&nbsp; Otherwise, return&nbsp;<code>-1</code>.</p>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>N = 2, trust = [[1,2]]
<strong>Output: </strong>2
</pre>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>N = 3, trust = [[1,3],[2,3]]
<strong>Output: </strong>3
</pre>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>N = 3, trust = [[1,3],[2,3],[3,1]]
<strong>Output: </strong>-1
</pre>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>N = 3, trust = [[1,2],[2,3]]
<strong>Output: </strong>-1
</pre>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>N = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]]
<strong>Output: </strong>3</pre>



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



<ol><li><code>1 &lt;= N &lt;= 1000</code></li><li><code>trust.length &lt;= 10000</code></li><li><code>trust[i]</code>&nbsp;are all different</li><li><code>trust[i][0] != trust[i][1]</code></li><li><code>1 &lt;= trust[i][0], trust[i][1] &lt;= N</code></li></ol>



<h2><strong>Solution: Degree</strong></h2>



<p>node with degree (in_degree &#8211; out_degree) N &#8211; 1 is the judge.</p>



<p>Time complexity: O(N+T)<br>Space complexity: O(N)</p>



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

<pre class="crayon-plain-tag">class Solution {
public:
  int findJudge(int N, vector&lt;vector&lt;int&gt;&gt;&amp; trusts) {    
    vector&lt;int&gt; degrees(N + 1);    
    for (const auto&amp; trust : trusts) {
      --degrees[trust[0]];
      ++degrees[trust[1]];
    }
    for (int i = 1; i &lt;= N; ++i)
      if (degrees[i] == N - 1) return i;
    return -1;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-997-find-the-town-judge/">花花酱 LeetCode 997. Find the Town Judge</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-997-find-the-town-judge/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 697. Degree of an Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-697-degree-of-an-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-697-degree-of-an-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 08 Mar 2018 16:32:32 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[degree]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[index]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2041</guid>

					<description><![CDATA[<p>题目大意：求&#8221;度&#8221;相同的最短子数组的长度。 Problem: https://leetcode.com/problems/degree-of-an-array/description/ Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-697-degree-of-an-array/">花花酱 LeetCode 697. Degree of 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>题目大意：求&#8221;度&#8221;相同的最短子数组的长度。</p>
<p><strong>Problem:</strong></p>
<p><a href="https://leetcode.com/problems/degree-of-an-array/description/">https://leetcode.com/problems/degree-of-an-array/description/</a></p>
<p>Given a non-empty array of non-negative integers <code>nums</code>, the <b>degree</b> of this array is defined as the maximum frequency of any one of its elements.</p>
<p>Your task is to find the smallest possible length of a (contiguous) subarray of <code>nums</code>, that has the same degree as <code>nums</code>.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input: [1, 2, 2, 3, 1]
Output: 2
Explanation: 
The input array has a degree of 2 because both elements 1 and 2 appear twice.
Of the subarrays that have the same degree:
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
The shortest length is 2. So return 2.</pre><p><b>Example 2:</b></p><pre class="crayon-plain-tag">Input: [1,2,2,3,1,4,2]
Output: 6</pre><p><strong>Solution 1: Hashtable</strong></p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(n)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 33 ms (beats 90.96%)
class Solution {
public:
  int findShortestSubArray(vector&lt;int&gt;&amp; nums) {
    unordered_map&lt;int, vector&lt;int&gt;&gt; indices;
    for (int i = 0; i &lt; nums.size(); ++i)
      indices[nums[i]].push_back(i);
    size_t degree = 0;
    for (const auto&amp; p : indices)
      degree = max(degree, p.second.size());
    int ans = nums.size();
    for (const auto&amp; p : indices) {
      if (p.second.size() != degree) continue;
      ans = min(ans, p.second.back() - p.second.front() + 1);
    }
    return ans;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-697-degree-of-an-array/">花花酱 LeetCode 697. Degree of 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/algorithms/array/leetcode-697-degree-of-an-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
