LeetCode-697-Degree of an Array-E

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: 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.

Example 2:

Input: [1,2,2,3,1,4,2]
Output: 6

解题思路:首先找出数组的度。
创建一个map,把出现过的数字作为map的第一维度,出现的数字在nums数组中所出现的地方作为第二维度(第二维度是一个vector)。找出第二维度中个数最多的第一维度数字,该数字出现的次数即为数组的度。
遍历map,在所有map的第二维度个数等于数组的度的地方,算出出现的最后一个地方和出现的第一个地方之差+1,即为符合要求的子数组的长度。并与minLen进行min运算,如此反复,最终得到最小子数组的长度。

int findShortestSubArray(vector<int>& nums) {
        int degree=0;   //nums数组的度
        unordered_map<int,vector<int>>mp;
        for(int i=0;i<nums.size();i++) 
            mp[nums[i]].push_back(i);
        for(auto it=mp.begin();it!=mp.end();it++)
            degree=max(degree,int(it->second.size()));

        int minLen=nums.size(); //符合要求的最小子数组的长度
        for(auto it=mp.begin();it!=mp.end();it++){
            if(it->second.size()==degree)
                minLen=min(minLen,it->second.back()-it->second[0]+1);
        }     
        return minLen;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值