<?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>index Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/index/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/index/</link>
	<description></description>
	<lastBuildDate>Thu, 08 Mar 2018 16:32:42 +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>index Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/index/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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>
