C++ easy understand beats 85%


  • 0
    B
    int findShortestSubArray(vector<int>& nums) {
        map<int,vector<int>> hash;int fre=0;
        for(int i=0;i<nums.size();i++)
        {
            if(hash.count(nums[i])==0)
            {
                hash[nums[i]]={1,i,i};
            }
            else
            {
                hash[nums[i]][0]++;
                hash[nums[i]][2]=i;
                
            }
            fre=max(fre,hash[nums[i]][0]);
        }
        int result=nums.size();
        for(auto x:hash)
        {
            if(x.second[0]==fre)result=min(result,x.second[2]-x.second[1]+1);
        }
        return result;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.