697. Degree of an Array
来源:互联网 发布:qq三国90js单刷孟获 编辑:程序博客网 时间:2024/05/17 01:02
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.
Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.
Example 1:Input: [1, 2, 2, 3, 1]Output: 2Explanation: 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.Example 2:Input: [1,2,2,3,1,4,2]Output: 6
方法一:
用hash table 统计所有number出现的次数,找出最高频率以及所有出现最高频率的数字,并依次计算他们在原数组中的左右边界,也就是第一次和最后一个出现的位置。
class Solution {public: int findShortestSubArray(vector<int>& nums) { unordered_map<int, int> hash; int maxfreq = 0; for (int i = 0; i < nums.size(); i++) { hash[nums[i]]++; maxfreq = max(hash[nums[i]], maxfreq); } if (maxfreq == 1) return 1; vector<int> max; for (auto m : hash) { if (m.second == maxfreq) { max.push_back(m.first); } } int minlen = nums.size(); for (int i = 0; i < max.size(); i++) { int cand = max[i]; int minId = -1; int maxId = -1; int freq = maxfreq; for (int j = 0; j < nums.size() && freq > 0; j++) { if (nums[j] == cand) { freq--; if (minId == -1) minId = j; if (freq == 0) maxId = j; } } if (maxId - minId + 1 < minlen) { minlen = maxId - minId + 1; } } return minlen; }};
方法二:
用空间换时间,减少遍历的次数:
多建立两个hash table 存储每一个number的左右边界下标,遍历一遍数组,建立三个hash table.
class Solution {public: int findShortestSubArray(vector<int>& nums) { unordered_map<int, int> hash_cnt; unordered_map<int, int> hash_left; unordered_map<int, int> hash_right; int maxfreq = 0; for (int i = 0; i < nums.size(); i++) { hash_cnt[nums[i]]++; maxfreq = max(hash_cnt[nums[i]], maxfreq); if (hash_left.find(nums[i]) == hash_left.end()) { hash_left[nums[i]] = i; } hash_right[nums[i]] = i; } if (maxfreq == 1) return 1; int minlen = nums.size(); for (auto m : hash_cnt) { if (m.second == maxfreq) { minlen = min(minlen, hash_right[m.first] - hash_left[m.first] + 1); } } return minlen; }};
阅读全文
1 0
- 697. Degree of an Array
- 697. Degree of an Array
- 697. Degree of an Array
- 697. Degree of an Array
- 697. Degree of an Array
- LWC 54:697. Degree of an Array
- LeetCode 697. Degree of an Array
- leetcode-697. Degree of an Array
- 【LeetCode】697. Degree of an Array
- LeetCode-697. Degree of an Array
- 697 Degree of an Array
- leetCode-Degree of an Array
- leetcode 697. Degree of an Array数组的度
- Degree of an Array问题及解法
- LeetCode697. Degree of an Array解答
- LeetCode.697 Degree of an Array
- leetcode 697[easy]---Degree of an Array
- LeetCode-697:Degree of an Array (度相同的最小子数组)
- 昨夜今晨最大八卦终于坐实——人类首次直接探测到了引力波
- LINUX安装实验报告
- numpy中dot、multiply和asterisk的区别和联系。
- 大数据产品推荐:易鑫大数据风控平台
- Unity3d鼠标拖拽物体实现任意角度自旋转
- 697. Degree of an Array
- springmvc事务控制批量插入多张表格
- @Generated源码翻译
- Spring Cloud Config-Git后端
- CSP 2017-09
- WPS2013开发工具中的VBA为灰色不可用状态的解决方法
- Deep Learning回顾之LeNet、AlexNet、GoogLeNet、VGG、ResNet
- 聚沙成塔--爬虫系列(四)(爬取糗事百科段子)
- Prolog教程