C++ O(n) easy solution


  • 0
    A

    '''
    #include<bits/c++.h>

    class Solution {
    public:
    int findShortestSubArray(vector<int>& nums) {
    unordered_map <int,vector <int>> m;
    for(int i=0;i<nums.size();i++){
    m[nums[i]].push_back(i);
    }
    int maxx=-1000;
    int val=0,v=0;
    unordered_map <int,vector <int>> :: iterator itr;
    for(itr=m.begin();itr!=m.end();itr++){
    cout<<itr->second.size()<<" "<<maxx<<endl;
    if(maxx < (int)itr->second.size()){
    maxx = itr->second.size();
    val = itr->second[itr->second.size()-1]-itr->second[0];
    }
    else if(maxx == (int)itr->second.size()){
    val = (val<itr->second[itr->second.size()-1]-itr->second[0])?val:itr->second[itr->second.size()-1]-itr->second[0];
    }
    }
    return val+1;
    }
    };'''


Log in to reply
 

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