You are viewing a single comment's thread. Return to all comments →
here is my solution
unordered_map<int, pair<int,int>> NumberToIdx; int MinDist = INT_MAX; for(int i = 0 ; i < a.size(); ++i){ if(NumberToIdx.find(a[i]) == NumberToIdx.end()){ NumberToIdx[a[i]] = make_pair(i,INT_MAX); } else{ if(NumberToIdx[a[i]].second == INT_MAX){ NumberToIdx[a[i]].second = i; MinDist = min(MinDist , (NumberToIdx[a[i]].second - NumberToIdx[a[i]].first)); } else{ int d = i - NumberToIdx[a[i]].second; MinDist = min(MinDist , d); } } } if(MinDist == INT_MAX) return -1; return MinDist;
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Distances
You are viewing a single comment's thread. Return to all comments →
here is my solution