{"id":2041,"date":"2018-03-08T08:32:32","date_gmt":"2018-03-08T16:32:32","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2041"},"modified":"2018-03-08T08:32:42","modified_gmt":"2018-03-08T16:32:42","slug":"leetcode-697-degree-of-an-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-697-degree-of-an-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 697. Degree of an Array"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u6c42&#8221;\u5ea6&#8221;\u76f8\u540c\u7684\u6700\u77ed\u5b50\u6570\u7ec4\u7684\u957f\u5ea6\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/degree-of-an-array\/description\/\">https:\/\/leetcode.com\/problems\/degree-of-an-array\/description\/<\/a><\/p>\n<p>Given a non-empty array of non-negative integers\u00a0<code>nums<\/code>, the\u00a0<b>degree<\/b>\u00a0of this array is defined as the maximum frequency of any one of its elements.<\/p>\n<p>Your task is to find the smallest possible length of a (contiguous) subarray of\u00a0<code>nums<\/code>, that has the same degree as\u00a0<code>nums<\/code>.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: [1, 2, 2, 3, 1]\r\nOutput: 2\r\nExplanation: \r\nThe input array has a degree of 2 because both elements 1 and 2 appear twice.\r\nOf the subarrays that have the same degree:\r\n[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]\r\nThe shortest length is 2. So return 2.\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: [1,2,2,3,1,4,2]\r\nOutput: 6<\/pre>\n<p><strong>Solution 1: Hashtable<\/strong><\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 33 ms (beats 90.96%)\r\nclass Solution {\r\npublic:\r\n  int findShortestSubArray(vector&lt;int&gt;&amp; nums) {\r\n    unordered_map&lt;int, vector&lt;int&gt;&gt; indices;\r\n    for (int i = 0; i &lt; nums.size(); ++i)\r\n      indices[nums[i]].push_back(i);\r\n    size_t degree = 0;\r\n    for (const auto&amp; p : indices)\r\n      degree = max(degree, p.second.size());\r\n    int ans = nums.size();\r\n    for (const auto&amp; p : indices) {\r\n      if (p.second.size() != degree) continue;\r\n      ans = min(ans, p.second.back() - p.second.front() + 1);\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u6c42&#8221;\u5ea6&#8221;\u76f8\u540c\u7684\u6700\u77ed\u5b50\u6570\u7ec4\u7684\u957f\u5ea6\u3002 Problem: https:\/\/leetcode.com\/problems\/degree-of-an-array\/description\/ Given a non-empty array of non-negative integers\u00a0nums, the\u00a0degree\u00a0of this array is defined as the maximum frequency of any one of its elements.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[244,82,245],"class_list":["post-2041","post","type-post","status-publish","format-standard","hentry","category-array","tag-degree","tag-hashtable","tag-index","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=2041"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2041\/revisions"}],"predecessor-version":[{"id":2043,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2041\/revisions\/2043"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}