You are viewing a single comment's thread. Return to all comments →
C++ O(n) solution:
int minimumDistances(vector<int> a) { unordered_map<int, int> num_last_idx; int min_distance {static_cast<int>(a.size())}; for (int i=0; i<a.size(); ++i){ if (num_last_idx.find(a[i]) != num_last_idx.end() && i-num_last_idx[a[i]] < min_distance) { min_distance = i-num_last_idx[a[i]]; } num_last_idx[a[i]] = i; } return (min_distance == a.size()) ? -1 : min_distance; }
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 →
C++ O(n) solution: