C++ solution


  • 0
    Z
        int findShortestSubArray(vector<int>& nums) {
            int max_cnt = 0, min_dis = 0;
            unordered_map<int, int> my_map;
            for(int i = 0; i < nums.size(); ++i) 
            {
                if(my_map.count(nums[i])) 
                {
                    int cnt = ++nums[my_map[nums[i]]], dis = i - my_map[nums[i]];
                    if(cnt > max_cnt) 
                    {
                        min_dis = dis;
                        max_cnt = cnt;
                    } 
                    else if(cnt == max_cnt && dis < min_dis) 
                    {
                        min_dis = dis;
                    }
                } 
                else
                {
                    my_map[nums[i]] = i;
                    nums[i] = 1;
                }
            }
            return min_dis + 1;
        }
    

Log in to reply
 

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